sigs.k8s.io/cluster-api-provider-aws@v1.5.5/cmd/clusterawsadm/controller/rollout/rollout.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rollout
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"os"
    24  	"time"
    25  
    26  	appsv1 "k8s.io/api/apps/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"k8s.io/apimachinery/pkg/util/strategicpatch"
    30  
    31  	"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/controller"
    32  )
    33  
    34  // ControllerDeploymentName is a tag for capa controller manager.
    35  const ControllerDeploymentName = "capa-controller-manager"
    36  
    37  // RolloutControllersInput defines the specs for rollout controllers input.
    38  type RolloutControllersInput struct {
    39  	KubeconfigPath    string
    40  	KubeconfigContext string
    41  	Namespace         string
    42  }
    43  
    44  // RolloutControllers initiates rollout restrart on the CAPA controller deployment.
    45  // Must be called after any change to the controller bootstrap secret.
    46  func RolloutControllers(input RolloutControllersInput) error {
    47  	client, err := controller.GetClient(input.KubeconfigPath, input.KubeconfigContext)
    48  	if err != nil {
    49  		fmt.Fprintf(os.Stderr, "Failed to get client-go client for the cluster: %s\n", err.Error())
    50  		return err
    51  	}
    52  
    53  	dp, err := client.AppsV1().Deployments(input.Namespace).Get(context.TODO(), ControllerDeploymentName, metav1.GetOptions{})
    54  	if err != nil {
    55  		fmt.Fprintf(os.Stderr, "Failed to get %s deployment: %s\n", ControllerDeploymentName, err.Error())
    56  		return err
    57  	}
    58  
    59  	initialDeployMarshalled, err := json.Marshal(dp)
    60  	if err != nil {
    61  		fmt.Fprintf(os.Stderr, "Failed to marshal controller deployment: %s\n", err.Error())
    62  		return err
    63  	}
    64  
    65  	updatedDeployment := dp.DeepCopy()
    66  	updatedDeployment.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339)
    67  	updatedDeployMarshalled, err := json.Marshal(updatedDeployment)
    68  	if err != nil {
    69  		fmt.Fprintf(os.Stderr, "Failed to marshal updated controller deployment: %s\n", err.Error())
    70  		return err
    71  	}
    72  
    73  	patch, err := strategicpatch.CreateTwoWayMergePatch(initialDeployMarshalled, updatedDeployMarshalled, appsv1.Deployment{})
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  
    78  	_, err = client.AppsV1().Deployments(input.Namespace).Patch(
    79  		context.TODO(),
    80  		ControllerDeploymentName,
    81  		types.StrategicMergePatchType,
    82  		patch,
    83  		metav1.PatchOptions{},
    84  	)
    85  	if err != nil {
    86  		fmt.Fprintf(os.Stderr, "Failed to initiate rollout restart on CAPA controller manager deployment: %s\n", err.Error())
    87  		return err
    88  	}
    89  	return nil
    90  }