Skip to content

Commit

Permalink
Merge pull request #17274 from zetaab/feat/fixrollingupdatetime
Browse files Browse the repository at this point in the history
make --admin configurable to rolling-update
  • Loading branch information
k8s-ci-robot authored Feb 24, 2025
2 parents 5bd925c + d5cea90 commit 7c52ef7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 7 additions & 0 deletions cmd/kops/rolling-update_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/instancegroups"
"k8s.io/kops/pkg/kubeconfig"
"k8s.io/kops/pkg/pretty"
"k8s.io/kops/pkg/validation"
"k8s.io/kops/upup/pkg/fi/cloudup"
Expand Down Expand Up @@ -145,6 +146,8 @@ type RollingUpdateOptions struct {

// TODO: Move more/all above options to RollingUpdateOptions
instancegroups.RollingUpdateOptions

kubeconfig.CreateKubecfgOptions
}

func (o *RollingUpdateOptions) InitDefaults() {
Expand All @@ -165,6 +168,8 @@ func (o *RollingUpdateOptions) InitDefaults() {

o.DrainTimeout = 15 * time.Minute

o.Admin = kubeconfig.DefaultKubecfgAdminLifetime

o.RollingUpdateOptions.InitDefaults()
}

Expand Down Expand Up @@ -193,6 +198,7 @@ func NewCmdRollingUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command {
cmd.Flags().BoolVar(&options.Force, "force", options.Force, "Force rolling update, even if no changes")
cmd.Flags().BoolVar(&options.CloudOnly, "cloudonly", options.CloudOnly, "Perform rolling update without validating cluster status (will cause downtime)")

cmd.Flags().DurationVar(&options.Admin, "admin", options.Admin, "a cluster admin user credential with the specified lifetime")
cmd.Flags().DurationVar(&options.ValidationTimeout, "validation-timeout", options.ValidationTimeout, "Maximum time to wait for a cluster to validate")
cmd.Flags().DurationVar(&options.DrainTimeout, "drain-timeout", options.DrainTimeout, "Maximum time to wait for a node to drain")
cmd.Flags().Int32Var(&options.ValidateCount, "validate-count", options.ValidateCount, "Number of times that a cluster needs to be validated after single node update")
Expand Down Expand Up @@ -227,6 +233,7 @@ func NewCmdRollingUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command {
}

func RunRollingUpdateCluster(ctx context.Context, f *util.Factory, out io.Writer, options *RollingUpdateOptions) error {
f.CreateKubecfgOptions = options.CreateKubecfgOptions
clientset, err := f.KopsClient()
if err != nil {
return err
Expand Down
17 changes: 10 additions & 7 deletions cmd/kops/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/url"
"strings"
"sync"
"time"

"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -56,6 +55,8 @@ type Factory struct {
mutex sync.Mutex
// clusters holds REST connection configuration for connecting to clusters
clusters map[string]*clusterInfo

kubeconfig.CreateKubecfgOptions
}

// clusterInfo holds REST connection configuration for connecting to a cluster
Expand All @@ -66,6 +67,7 @@ type clusterInfo struct {
cachedHTTPClient *http.Client
cachedRESTConfig *rest.Config
cachedDynamicClient dynamic.Interface
kubeconfig.CreateKubecfgOptions
}

func NewFactory(options *FactoryOptions) *Factory {
Expand Down Expand Up @@ -177,14 +179,15 @@ func (f *Factory) getClusterInfo(cluster *kops.Cluster) *clusterInfo {

func (f *Factory) RESTConfig(cluster *kops.Cluster) (*rest.Config, error) {
clusterInfo := f.getClusterInfo(cluster)
clusterInfo.CreateKubecfgOptions = f.CreateKubecfgOptions
return clusterInfo.RESTConfig()
}

func (f *clusterInfo) RESTConfig() (*rest.Config, error) {
ctx := context.Background()

if f.cachedRESTConfig == nil {
restConfig, err := f.factory.buildRESTConfig(ctx, f.cluster)
restConfig, err := f.factory.buildRESTConfig(ctx, f.cluster, f.CreateKubecfgOptions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -253,7 +256,7 @@ func (f *Factory) VFSContext() *vfs.VFSContext {
return f.vfsContext
}

func (f *Factory) buildRESTConfig(ctx context.Context, cluster *kops.Cluster) (*rest.Config, error) {
func (f *Factory) buildRESTConfig(ctx context.Context, cluster *kops.Cluster, options kubeconfig.CreateKubecfgOptions) (*rest.Config, error) {
clientset, err := f.KopsClient()
if err != nil {
return nil, err
Expand All @@ -274,9 +277,9 @@ func (f *Factory) buildRESTConfig(ctx context.Context, cluster *kops.Cluster) (*
return nil, err
}

// Generate a relatively short-lived certificate / kubeconfig
createKubecfgOptions := kubeconfig.CreateKubecfgOptions{
Admin: 1 * time.Hour,
// backwards compatibility
if options.Admin == 0 {
options.Admin = kubeconfig.DefaultKubecfgAdminLifetime
}

conf, err := kubeconfig.BuildKubecfg(
Expand All @@ -285,7 +288,7 @@ func (f *Factory) buildRESTConfig(ctx context.Context, cluster *kops.Cluster) (*
keyStore,
secretStore,
cloud,
createKubecfgOptions,
options,
f.KopsStateStore())
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions docs/cli/kops_rolling-update_cluster.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c52ef7

Please sign in to comment.