1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
22
"github.com/prometheus/prometheus/util/strutil"
23
v1 "k8s.io/api/core/v1"
24
"k8s.io/apimachinery/pkg/api/errors"
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
"k8s.io/apimachinery/pkg/runtime/schema"
27
"k8s.io/client-go/kubernetes"
30
iopv1alpha1 "istio.io/istio/operator/pkg/apis/istio/v1alpha1"
31
"istio.io/istio/pkg/config/constants"
32
"istio.io/istio/pkg/kube"
35
// GKString differs from default representation of GroupKind
36
func GKString(gvk schema.GroupKind) string {
37
return fmt.Sprintf("%s/%s", gvk.Group, gvk.Kind)
40
// ValidateIOPCAConfig validates if the IstioOperator CA configs are applicable to the K8s cluster
41
func ValidateIOPCAConfig(client kube.Client, iop *iopv1alpha1.IstioOperator) error {
42
globalI := iop.Spec.Values.AsMap()["global"]
43
global, ok := globalI.(map[string]any)
45
// This means no explicit global configuration. Still okay
48
ca, ok := global["pilotCertProvider"].(string)
50
// This means the default pilotCertProvider is being used
53
if ca == "kubernetes" {
54
ver, err := client.GetKubernetesVersion()
56
return fmt.Errorf("failed to determine support for K8s legacy signer. Use the --force flag to ignore this: %v", err)
59
if kube.IsAtLeastVersion(client, 22) {
60
return fmt.Errorf("configuration PILOT_CERT_PROVIDER=%s not supported in Kubernetes %v."+
61
"Please pick another value for PILOT_CERT_PROVIDER", ca, ver.String())
67
// CreateNamespace creates a namespace using the given k8s interface.
68
func CreateNamespace(cs kubernetes.Interface, namespace string, network string, dryRun bool) error {
70
scope.Infof("Not applying Namespace %s because of dry run.", namespace)
74
// Setup default namespace
75
namespace = constants.IstioSystemNamespace
77
// check if the namespace already exists. If yes, do nothing. If no, create a new one.
78
if _, err := cs.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{}); err != nil {
79
if errors.IsNotFound(err) {
80
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{
82
Labels: map[string]string{},
85
ns.Labels[label.TopologyNetwork.Name] = network
87
_, err := cs.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
89
return fmt.Errorf("failed to create namespace %v: %v", namespace, err)
95
return fmt.Errorf("failed to check if namespace %v exists: %v", namespace, err)
101
func PrometheusPathAndPort(pod *v1.Pod) (string, int, error) {
104
for key, val := range pod.ObjectMeta.Annotations {
105
switch strutil.SanitizeLabelName(key) {
106
case "prometheus_io_port":
107
p, err := strconv.Atoi(val)
109
return "", 0, fmt.Errorf("failed to parse port from annotation: %v", err)
113
case "prometheus_io_path":
118
return path, port, nil