github.com/SUSE/skuba@v1.4.17/pkg/skuba/actions/addon/upgrade/apply.go (about)

     1  /*
     2   * Copyright (c) 2019 SUSE LLC.
     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  
    18  package addons
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/pkg/errors"
    24  	clientset "k8s.io/client-go/kubernetes"
    25  
    26  	"github.com/SUSE/skuba/internal/pkg/skuba/addons"
    27  	"github.com/SUSE/skuba/internal/pkg/skuba/kubeadm"
    28  	"github.com/SUSE/skuba/internal/pkg/skuba/kubernetes"
    29  	"github.com/SUSE/skuba/internal/pkg/skuba/replica"
    30  	"github.com/SUSE/skuba/internal/pkg/skuba/upgrade/addon"
    31  )
    32  
    33  // Apply implements the `skuba addon upgrade apply` command.
    34  func Apply(client clientset.Interface) error {
    35  	currentClusterVersion, err := kubeadm.GetCurrentClusterVersion(client)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	clusterConfiguration, err := kubeadm.GetClusterConfiguration(client)
    41  	if err != nil {
    42  		return errors.Wrap(err, "[apply] Could not fetch cluster configuration")
    43  	}
    44  
    45  	addonConfiguration := addons.AddonConfiguration{
    46  		ClusterVersion: currentClusterVersion,
    47  		ControlPlane:   clusterConfiguration.ControlPlaneEndpoint,
    48  		ClusterName:    clusterConfiguration.ClusterName,
    49  	}
    50  
    51  	// check local addons cluster folder configuration is up-to-date
    52  	match, err := addons.CheckLocalAddonsBaseManifests(addonConfiguration)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if !match {
    57  		fmt.Println("Current local addons cluster folder configuration is out-of-date.")
    58  		fmt.Println("Please run \"skuba addon refresh localconfig\" before you perform addon upgrade.")
    59  		return nil
    60  	}
    61  
    62  	currentVersion := currentClusterVersion.String()
    63  	latestVersion := kubernetes.LatestVersion().String()
    64  	allNodesVersioningInfo, err := kubernetes.AllNodesVersioningInfo(client)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	allNodesMatchClusterVersion := kubernetes.AllNodesTolerateClusterVersionWithVersioningInfo(allNodesVersioningInfo, currentClusterVersion)
    69  	fmt.Printf("Current Kubernetes cluster version: %s\n", currentVersion)
    70  	fmt.Printf("Latest Kubernetes version: %s\n", latestVersion)
    71  	fmt.Println()
    72  
    73  	if !allNodesMatchClusterVersion {
    74  		return errors.Errorf("[apply] Not all nodes match clusterVersion %s", currentVersion)
    75  	}
    76  
    77  	updatedAddons, err := addon.UpdatedAddons(client, currentClusterVersion)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if addon.HasAddonUpdate(updatedAddons) {
    83  		dryRun := false
    84  		if err := addons.DeployAddons(client, addonConfiguration, dryRun); err != nil {
    85  			return errors.Wrap(err, "[apply] Failed to deploy addons")
    86  		}
    87  
    88  		replicaHelper, err := replica.NewHelper(client)
    89  		if err != nil {
    90  			return err
    91  		}
    92  		if err := replicaHelper.UpdateNodes(); err != nil {
    93  			return err
    94  		}
    95  
    96  		fmt.Println("[apply] Successfully upgraded addons")
    97  	} else {
    98  		fmt.Printf("[apply] Congratulations! Addons for %s are already at the latest version available\n", currentVersion)
    99  	}
   100  
   101  	return nil
   102  }