...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/cmd/gke_addon_poststart/main.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/cmd/gke_addon_poststart

     1  // Copyright 2022 Google LLC
     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 main
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"time"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/apimachinery/pkg/util/wait"
    27  	"k8s.io/client-go/dynamic"
    28  	"k8s.io/client-go/util/retry"
    29  	ctrl "sigs.k8s.io/controller-runtime"
    30  	"sigs.k8s.io/yaml"
    31  )
    32  
    33  const defaultConfigConnector = `
    34  apiVersion: core.cnrm.cloud.google.com/v1beta1
    35  kind: ConfigConnector
    36  metadata:
    37    name: configconnector.core.cnrm.cloud.google.com
    38  spec:
    39    mode: namespaced
    40  `
    41  
    42  var configConnectorResource = schema.GroupVersionResource{
    43  	Group:    "core.cnrm.cloud.google.com",
    44  	Version:  "v1beta1",
    45  	Resource: "configconnectors",
    46  }
    47  
    48  // This program exists to do the work that needs to be done for the GKE add-on
    49  // after the manager container is created. This is meant to be used for the GKE
    50  // add-on only, not the standalone operator.
    51  func main() {
    52  	ctx := context.Background()
    53  	dynamicClient, err := dynamic.NewForConfig(ctrl.GetConfigOrDie())
    54  	if err != nil {
    55  		log.Fatalf("error creating dynamic client: %v", err)
    56  	}
    57  	if err := createDefaultConfigConnector(ctx, dynamicClient); err != nil {
    58  		log.Fatalf("error creating default ConfigConnector object: %v", err)
    59  	}
    60  }
    61  
    62  // createDefaultConfigConnector creates a ConfigConnector object on the K8s API
    63  // server. This is done for users who want to have a default ConfigConnector
    64  // object created for them upon enabling the GKE add-on.
    65  func createDefaultConfigConnector(ctx context.Context, dynamicClient dynamic.Interface) error {
    66  	u := &unstructured.Unstructured{}
    67  	b := []byte(defaultConfigConnector)
    68  	if err := yaml.Unmarshal(b, u); err != nil {
    69  		return fmt.Errorf("error unmarshalling bytes to unstruct: %v", err)
    70  	}
    71  
    72  	// Create the ConfigConnector object. Retry on error just in case the
    73  	// ConfigConnector CRD does not exist yet.
    74  	backoff := wait.Backoff{
    75  		// Make 10 attempts with 1s delay between each attempt.
    76  		Steps:    10,
    77  		Duration: 1 * time.Second,
    78  		Factor:   1.0,
    79  	}
    80  	retriable := func(_ error) bool {
    81  		// Retry on all errors.
    82  		return true
    83  	}
    84  	return retry.OnError(backoff, retriable, func() error {
    85  		// Terminate in case a ConfigConnector object already exists.
    86  		_, err := dynamicClient.Resource(configConnectorResource).Get(ctx, u.GetName(), metav1.GetOptions{})
    87  		if err == nil {
    88  			return nil
    89  		}
    90  
    91  		_, err = dynamicClient.Resource(configConnectorResource).Create(ctx, u, metav1.CreateOptions{})
    92  		return err
    93  	})
    94  }
    95  

View as plain text