github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/alpha/rollouts/advance/advance.go (about)

     1  // Copyright 2023 The kpt 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 advance
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/GoogleContainerTools/kpt/commands/alpha/rollouts/rolloutsclient"
    22  	"github.com/GoogleContainerTools/kpt/rollouts/api/v1alpha1"
    23  	"github.com/spf13/cobra"
    24  	k8scmdutil "k8s.io/kubectl/pkg/cmd/util"
    25  )
    26  
    27  func newRunner(ctx context.Context, f k8scmdutil.Factory) *runner {
    28  	r := &runner{
    29  		ctx:     ctx,
    30  		factory: f,
    31  	}
    32  	c := &cobra.Command{
    33  		Use:     "advance rollout-name wave-name",
    34  		Short:   "advances the wave of a progressive rollout",
    35  		Long:    "advances the wave of a progressive rollout",
    36  		Example: "advances the wave of a progressive rollout",
    37  		RunE:    r.runE,
    38  	}
    39  	r.Command = c
    40  	return r
    41  }
    42  
    43  func NewCommand(ctx context.Context, f k8scmdutil.Factory) *cobra.Command {
    44  	return newRunner(ctx, f).Command
    45  }
    46  
    47  type runner struct {
    48  	ctx     context.Context
    49  	Command *cobra.Command
    50  	factory k8scmdutil.Factory
    51  }
    52  
    53  func (r *runner) runE(_ *cobra.Command, args []string) error {
    54  	rlc, err := rolloutsclient.New()
    55  	if err != nil {
    56  		fmt.Printf("%s\n", err)
    57  		return err
    58  	}
    59  
    60  	if len(args) == 0 {
    61  		return fmt.Errorf("must provide rollout name")
    62  	}
    63  
    64  	if len(args) == 1 {
    65  		return fmt.Errorf("must provide wave name")
    66  	}
    67  
    68  	rolloutName := args[0]
    69  	waveName := args[1]
    70  
    71  	namespace, _, err := r.factory.ToRawKubeConfigLoader().Namespace()
    72  	if err != nil {
    73  		return err
    74  	}
    75  	rollout, err := rlc.Get(r.ctx, namespace, rolloutName)
    76  	if err != nil {
    77  		fmt.Printf("%s\n", err)
    78  		return err
    79  	}
    80  
    81  	if rollout.Spec.Strategy.Type != v1alpha1.Progressive {
    82  		return fmt.Errorf("rollout must be using the progressive strategy to use this command")
    83  	}
    84  
    85  	if rollout.Status.WaveStatuses != nil {
    86  		waveFound := false
    87  
    88  		for _, waveStatus := range rollout.Status.WaveStatuses {
    89  			if waveStatus.Name == waveName {
    90  				waveFound = true
    91  				break
    92  			}
    93  		}
    94  
    95  		if !waveFound {
    96  			return fmt.Errorf("wave %q not found in this rollout", waveName)
    97  		}
    98  	}
    99  
   100  	rollout.Spec.Strategy.Progressive.PauseAfterWave.WaveName = waveName
   101  
   102  	err = rlc.Update(r.ctx, rollout)
   103  	if err != nil {
   104  		fmt.Printf("%s\n", err)
   105  		return err
   106  	}
   107  
   108  	fmt.Println("done")
   109  	return nil
   110  }