Skip to content

Commit

Permalink
fix: handle incorrect cluster RESTconfig without panic
Browse files Browse the repository at this point in the history
  • Loading branch information
CefBoud committed Oct 1, 2024
1 parent 5f23bb6 commit 04ac06e
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 22 deletions.
4 changes: 3 additions & 1 deletion cmd/argocd/commands/admin/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ argocd admin cluster kubeconfig https://cluster-api-url:6443 /path/to/output/kub

cluster, err := db.NewDB(namespace, settings.NewSettingsManager(ctx, kubeclientset, namespace), kubeclientset).GetCluster(ctx, serverUrl)
errors.CheckError(err)
err = kube.WriteKubeConfig(cluster.RawRestConfig(), namespace, output)
rawConfig, err := cluster.RawRestConfig()
errors.CheckError(err)
err = kube.WriteKubeConfig(rawConfig, namespace, output)
errors.CheckError(err)
},
}
Expand Down
6 changes: 5 additions & 1 deletion controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,11 @@ func (ctrl *ApplicationController) finalizeApplicationDeletion(app *appv1.Applic
logCtx.Infof("Resource entries removed from undefined cluster")
return nil
}
config := metrics.AddMetricsTransportWrapper(ctrl.metricsServer, app, cluster.RESTConfig())
clusterRESTConfig, err := cluster.RESTConfig()
if err != nil {
return err
}
config := metrics.AddMetricsTransportWrapper(ctrl.metricsServer, app, clusterRESTConfig)

if app.CascadedDeletion() {
logCtx.Infof("Deleting resources")
Expand Down
12 changes: 10 additions & 2 deletions controller/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,10 @@ func (c *liveStateCache) getCluster(server string) (clustercache.ClusterCache, e
return nil, fmt.Errorf("error getting value for %v: %w", settings.RespectRBAC, err)
}

clusterCacheConfig := cluster.RESTConfig()
clusterCacheConfig, err := cluster.RESTConfig()
if err != nil {
return nil, fmt.Errorf("error getting cluster RESTConfig: %w", err)
}
// Controller dynamically fetches all resource types available on the cluster
// using a discovery API that may contain deprecated APIs.
// This causes log flooding when managing a large number of clusters.
Expand Down Expand Up @@ -821,7 +824,12 @@ func (c *liveStateCache) handleModEvent(oldCluster *appv1.Cluster, newCluster *a

var updateSettings []clustercache.UpdateSettingsFunc
if !reflect.DeepEqual(oldCluster.Config, newCluster.Config) {
updateSettings = append(updateSettings, clustercache.SetConfig(newCluster.RESTConfig()))
newClusterRESTConfig, err := newCluster.RESTConfig()
if err == nil {
updateSettings = append(updateSettings, clustercache.SetConfig(newClusterRESTConfig))
} else {
log.Errorf("error getting cluster REST config: %v", err)
}
}
if !reflect.DeepEqual(oldCluster.Namespaces, newCluster.Namespaces) {
updateSettings = append(updateSettings, clustercache.SetNamespaces(newCluster.Namespaces))
Expand Down
23 changes: 20 additions & 3 deletions controller/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ func (m *appStateManager) getResourceOperations(server string) (kube.ResourceOpe
if err != nil {
return nil, nil, fmt.Errorf("error getting cluster: %w", err)
}
ops, cleanup, err := m.kubectl.ManageResources(cluster.RawRestConfig(), clusterCache.GetOpenAPISchema())

rawConfig, err := cluster.RawRestConfig()
if err != nil {
return nil, nil, fmt.Errorf("error getting cluster REST config: %w", err)
}
ops, cleanup, err := m.kubectl.ManageResources(rawConfig, clusterCache.GetOpenAPISchema())
if err != nil {
return nil, nil, fmt.Errorf("error creating kubectl ResourceOperations: %w", err)
}
Expand Down Expand Up @@ -213,8 +218,20 @@ func (m *appStateManager) SyncAppState(app *v1alpha1.Application, state *v1alpha
return
}

rawConfig := clst.RawRestConfig()
restConfig := metrics.AddMetricsTransportWrapper(m.metricsServer, app, clst.RESTConfig())
rawConfig, err := clst.RawRestConfig()
if err != nil {
state.Phase = common.OperationError
state.Message = err.Error()
return
}

clusterRESTConfig, err := clst.RESTConfig()
if err != nil {
state.Phase = common.OperationError
state.Message = err.Error()
return
}
restConfig := metrics.AddMetricsTransportWrapper(m.metricsServer, app, clusterRESTConfig)

resourceOverrides, err := m.settingsMgr.GetResourceOverrides()
if err != nil {
Expand Down
19 changes: 11 additions & 8 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,7 @@ func SetK8SConfigDefaults(config *rest.Config) error {
}

// RawRestConfig returns a go-client REST config from cluster that might be serialized into the file using kube.WriteKubeConfig method.
func (c *Cluster) RawRestConfig() *rest.Config {
func (c *Cluster) RawRestConfig() (*rest.Config, error) {
var config *rest.Config
var err error
if c.Server == KubernetesInternalAPIServerAddr && env.ParseBoolFromEnv(EnvVarFakeInClusterConfig, false) {
Expand Down Expand Up @@ -3182,22 +3182,25 @@ func (c *Cluster) RawRestConfig() *rest.Config {
}
}
if err != nil {
panic(fmt.Sprintf("Unable to create K8s REST config: %v", err))
return nil, fmt.Errorf("Unable to create K8s REST config: %w", err)
}
config.Timeout = K8sServerSideTimeout
config.QPS = K8sClientConfigQPS
config.Burst = K8sClientConfigBurst
return config
return config, nil
}

// RESTConfig returns a go-client REST config from cluster with tuned throttling and HTTP client settings.
func (c *Cluster) RESTConfig() *rest.Config {
config := c.RawRestConfig()
err := SetK8SConfigDefaults(config)
func (c *Cluster) RESTConfig() (*rest.Config, error) {
config, err := c.RawRestConfig()
if err != nil {
panic(fmt.Sprintf("Unable to apply K8s REST config defaults: %v", err))
return nil, fmt.Errorf("Unable to get K8s RAW REST config: %w", err)
}
return config
err = SetK8SConfigDefaults(config)
if err != nil {
return nil, fmt.Errorf("Unable to apply K8s REST config defaults: %w", err)
}
return config, nil
}

// UnmarshalToUnstructured unmarshals a resource representation in JSON to unstructured data
Expand Down
6 changes: 5 additions & 1 deletion server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,11 @@ func (s *Server) getApplicationClusterConfig(ctx context.Context, a *appv1.Appli
if err != nil {
return nil, fmt.Errorf("error getting cluster: %w", err)
}
config := clst.RESTConfig()
config, err := clst.RESTConfig()
if err != nil {
return nil, fmt.Errorf("error getting cluster REST config: %w", err)
}

return config, err
}

Expand Down
6 changes: 5 additions & 1 deletion server/application/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func (s *terminalHandler) getApplicationClusterRawConfig(ctx context.Context, a
if err != nil {
return nil, err
}
return clst.RawRestConfig(), nil
rawConfig, err := clst.RawRestConfig()
if err != nil {
return nil, err
}
return rawConfig, nil
}

type GetSettingsFunc func() (*settings.ArgoCDSettings, error)
Expand Down
24 changes: 20 additions & 4 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func (s *Server) Create(ctx context.Context, q *cluster.ClusterCreateRequest) (*
return nil, err
}
c := q.Cluster
serverVersion, err := s.kubectl.GetServerVersion(c.RESTConfig())
clusterRESTConfig, err := c.RESTConfig()
if err != nil {
return nil, err
}

serverVersion, err := s.kubectl.GetServerVersion(clusterRESTConfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -313,9 +318,13 @@ func (s *Server) Update(ctx context.Context, q *cluster.ClusterUpdateRequest) (*
}
q.Cluster = c
}
clusterRESTConfig, err := q.Cluster.RESTConfig()
if err != nil {
return nil, err
}

// Test the token we just created before persisting it
serverVersion, err := s.kubectl.GetServerVersion(q.Cluster.RESTConfig())
serverVersion, err := s.kubectl.GetServerVersion(clusterRESTConfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -406,7 +415,10 @@ func (s *Server) RotateAuth(ctx context.Context, q *cluster.ClusterQuery) (*clus
for _, server := range servers {
logCtx := log.WithField("cluster", server)
logCtx.Info("Rotating auth")
restCfg := clust.RESTConfig()
restCfg, err := clust.RESTConfig()
if err != nil {
return nil, err
}
if restCfg.BearerToken == "" {
return nil, status.Errorf(codes.InvalidArgument, "Cluster '%s' does not use bearer token authentication", server)
}
Expand All @@ -428,8 +440,12 @@ func (s *Server) RotateAuth(ctx context.Context, q *cluster.ClusterQuery) (*clus
clust.Config.CertData = nil
clust.Config.BearerToken = string(newSecret.Data["token"])

clusterRESTConfig, err := clust.RESTConfig()
if err != nil {
return nil, err
}
// Test the token we just created before persisting it
serverVersion, err := s.kubectl.GetServerVersion(clust.RESTConfig())
serverVersion, err := s.kubectl.GetServerVersion(clusterRESTConfig)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ func ValidateRepo(
})
return conditions, nil
}
config := cluster.RESTConfig()
config, err := cluster.RESTConfig()
if err != nil {
return nil, fmt.Errorf("error getting cluster REST config: %w", err)
}
// nolint:staticcheck
cluster.ServerVersion, err = kubectl.GetServerVersion(config)
if err != nil {
Expand Down

0 comments on commit 04ac06e

Please sign in to comment.