istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/cmd/mesh/operator-common.go (about)

     1  // Copyright Istio Authors
     2  //
     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
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     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.
    14  
    15  package mesh
    16  
    17  import (
    18  	"context"
    19  
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/client-go/kubernetes"
    22  	_ "k8s.io/client-go/plugin/pkg/client/auth" //  Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
    23  
    24  	"istio.io/istio/operator/pkg/helm"
    25  	"istio.io/istio/operator/pkg/name"
    26  	"istio.io/istio/operator/pkg/util"
    27  )
    28  
    29  type operatorCommonArgs struct {
    30  	// hub is the hub for the operator image.
    31  	hub string
    32  	// tag is the tag for the operator image.
    33  	tag string
    34  	// imagePullSecrets is an array of imagePullSecret to pull operator image from the private registry
    35  	imagePullSecrets []string
    36  	// operatorNamespace is the namespace the operator controller is installed into.
    37  	operatorNamespace string
    38  	// watchedNamespaces is the namespaces the operator controller watches, could be namespace list separated by comma.
    39  	watchedNamespaces string
    40  	// istioNamespace is deprecated, use watchedNamespaces instead.
    41  	istioNamespace string
    42  	// manifestsPath is a path to a charts and profiles directory in the local filesystem with a release tgz.
    43  	manifestsPath string
    44  	// revision is the Istio control plane revision the command targets.
    45  	revision string
    46  	// outputFormat controls the format of operator dumps
    47  	outputFormat string
    48  }
    49  
    50  const (
    51  	operatorResourceName     = "istio-operator"
    52  	operatorDefaultNamespace = "istio-operator"
    53  )
    54  
    55  // isControllerInstalled reports whether an operator deployment exists in the given namespace.
    56  func isControllerInstalled(cs kubernetes.Interface, operatorNamespace string, revision string) (bool, error) {
    57  	orn := operatorResourceName
    58  	if revision != "" {
    59  		orn += "-" + revision
    60  	}
    61  	return deploymentExists(cs, operatorNamespace, orn)
    62  }
    63  
    64  // renderOperatorManifest renders a manifest to install the operator with the given input arguments.
    65  func renderOperatorManifest(_ *RootArgs, ocArgs *operatorCommonArgs) (string, string, error) {
    66  	r := helm.NewHelmRenderer(ocArgs.manifestsPath, "istio-operator", string(name.IstioOperatorComponentName), ocArgs.operatorNamespace, nil)
    67  
    68  	if err := r.Run(); err != nil {
    69  		return "", "", err
    70  	}
    71  
    72  	tmpl := `
    73  istioNamespace: {{.IstioNamespace}}
    74  watchedNamespaces: {{.WatchedNamespaces}}
    75  hub: {{.Hub}}
    76  tag: {{.Tag}}
    77  {{- if .ImagePullSecrets }}
    78  imagePullSecrets:
    79  {{- range .ImagePullSecrets }}
    80  - {{ . }}
    81  {{- end }}
    82  {{- end }}
    83  revision: {{if .Revision }} {{.Revision}} {{else}} "" {{end}}
    84  `
    85  
    86  	tv := struct {
    87  		IstioNamespace    string
    88  		WatchedNamespaces string
    89  		Hub               string
    90  		Tag               string
    91  		ImagePullSecrets  []string
    92  		Revision          string
    93  	}{
    94  		IstioNamespace:    ocArgs.istioNamespace,
    95  		WatchedNamespaces: ocArgs.watchedNamespaces,
    96  		Hub:               ocArgs.hub,
    97  		Tag:               ocArgs.tag,
    98  		ImagePullSecrets:  ocArgs.imagePullSecrets,
    99  		Revision:          ocArgs.revision,
   100  	}
   101  	vals, err := util.RenderTemplate(tmpl, tv)
   102  	if err != nil {
   103  		return "", "", err
   104  	}
   105  	manifest, err := r.RenderManifest(vals)
   106  	return vals, manifest, err
   107  }
   108  
   109  // deploymentExists returns true if the given deployment in the namespace exists.
   110  func deploymentExists(cs kubernetes.Interface, namespace, name string) (bool, error) {
   111  	d, err := cs.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
   112  	if err != nil {
   113  		return false, err
   114  	}
   115  	return d != nil, nil
   116  }