Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(in-cluster): do not allow the cluster to be used when disabled #21208

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions util/db/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,19 @@ func (db *db) WatchClusters(ctx context.Context,
handleModEvent func(oldCluster *appv1.Cluster, newCluster *appv1.Cluster),
handleDeleteEvent func(clusterServer string),
) error {
localCls, err := db.GetCluster(ctx, appv1.KubernetesInternalAPIServerAddr)
argoSettings, err := db.settingsMgr.GetSettings()
if err != nil {
return err
}
handleAddEvent(localCls)

localCls := db.getLocalCluster()
if argoSettings.InClusterEnabled {
localCls, err = db.GetCluster(ctx, appv1.KubernetesInternalAPIServerAddr)
if err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap the error?

}
handleAddEvent(localCls)
}

db.watchSecrets(
ctx,
Expand All @@ -159,9 +167,11 @@ func (db *db) WatchClusters(ctx context.Context,
return
}
if cluster.Server == appv1.KubernetesInternalAPIServerAddr {
// change local cluster event to modified or deleted, since it cannot be re-added or deleted
handleModEvent(localCls, cluster)
localCls = cluster
if argoSettings.InClusterEnabled {
// change local cluster event to modified, since it cannot be added at runtime
handleModEvent(localCls, cluster)
localCls = cluster
}
return
}
handleAddEvent(cluster)
Expand All @@ -185,10 +195,11 @@ func (db *db) WatchClusters(ctx context.Context,
},

func(secret *apiv1.Secret) {
if string(secret.Data["server"]) == appv1.KubernetesInternalAPIServerAddr {
// change local cluster event to modified or deleted, since it cannot be re-added or deleted
handleModEvent(localCls, db.getLocalCluster())
localCls = db.getLocalCluster()
if string(secret.Data["server"]) == appv1.KubernetesInternalAPIServerAddr && argoSettings.InClusterEnabled {
// change local cluster event to modified, since it cannot be deleted at runtime, unless disabled.
newLocalCls := db.getLocalCluster()
handleModEvent(localCls, newLocalCls)
localCls = newLocalCls
} else {
handleDeleteEvent(string(secret.Data["server"]))
}
Expand All @@ -214,6 +225,14 @@ func (db *db) getClusterSecret(server string) (*apiv1.Secret, error) {

// GetCluster returns a cluster from a query
func (db *db) GetCluster(_ context.Context, server string) (*appv1.Cluster, error) {
argoSettings, err := db.settingsMgr.GetSettings()
if err != nil {
return nil, err
}
if server == appv1.KubernetesInternalAPIServerAddr && !argoSettings.InClusterEnabled {
return nil, status.Errorf(codes.NotFound, "cluster %q is disabled", server)
}

informer, err := db.settingsMgr.GetSecretsInformer()
if err != nil {
return nil, err
Expand All @@ -222,6 +241,7 @@ func (db *db) GetCluster(_ context.Context, server string) (*appv1.Cluster, erro
if err != nil {
return nil, err
}

if len(res) > 0 {
return SecretToCluster(res[0].(*apiv1.Secret))
}
Expand Down Expand Up @@ -254,6 +274,10 @@ func (db *db) GetProjectClusters(ctx context.Context, project string) ([]*appv1.
}

func (db *db) GetClusterServersByName(ctx context.Context, name string) ([]string, error) {
argoSettings, err := db.settingsMgr.GetSettings()
if err != nil {
return nil, err
}
informer, err := db.settingsMgr.GetSecretsInformer()
if err != nil {
return nil, err
Expand All @@ -265,7 +289,7 @@ func (db *db) GetClusterServersByName(ctx context.Context, name string) ([]strin
return nil, err
}

if len(localClusterSecrets) == 0 && db.getLocalCluster().Name == name {
if len(localClusterSecrets) == 0 && db.getLocalCluster().Name == name && argoSettings.InClusterEnabled {
return []string{appv1.KubernetesInternalAPIServerAddr}, nil
}

Expand All @@ -276,7 +300,11 @@ func (db *db) GetClusterServersByName(ctx context.Context, name string) ([]strin
var res []string
for i := range secrets {
s := secrets[i].(*apiv1.Secret)
res = append(res, strings.TrimRight(string(s.Data["server"]), "/"))
server := strings.TrimRight(string(s.Data["server"]), "/")
if !argoSettings.InClusterEnabled && server == appv1.KubernetesInternalAPIServerAddr {
continue
}
res = append(res, server)
}
return res, nil
}
Expand Down
44 changes: 42 additions & 2 deletions util/db/cluster_norace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestWatchClusters_CreateRemoveCluster(t *testing.T) {
kubeclientset := fake.NewClientset(emptyArgoCDConfigMap, argoCDSecret)
settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace)
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
completed := runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
assert.Nil(t, old)
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
Expand All @@ -72,6 +72,7 @@ func TestWatchClusters_CreateRemoveCluster(t *testing.T) {
assert.Equal(t, "https://minikube", old.Server)
},
})
assert.True(t, completed, "Failed due to timeout")
}

func TestWatchClusters_LocalClusterModifications(t *testing.T) {
Expand Down Expand Up @@ -104,7 +105,7 @@ func TestWatchClusters_LocalClusterModifications(t *testing.T) {
kubeclientset := fake.NewClientset(emptyArgoCDConfigMap, argoCDSecret)
settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace)
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
completed := runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
assert.Nil(t, old)
assert.Equal(t, v1alpha1.KubernetesInternalAPIServerAddr, new.Server)
Expand All @@ -127,4 +128,43 @@ func TestWatchClusters_LocalClusterModifications(t *testing.T) {
assert.Equal(t, "in-cluster", new.Name)
},
})
assert.True(t, completed, "Failed due to timeout")
}

func TestWatchClusters_LocalClusterModificationsWhenDisabled(t *testing.T) {
// !race:
// Intermittent failure when running TestWatchClusters_LocalClusterModifications with -race, likely due to race condition
// https://github.com/argoproj/argo-cd/issues/4755
argoCDConfigMapWithInClusterServerAddressDisabled := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: common.ArgoCDConfigMapName,
Namespace: fakeNamespace,
Labels: map[string]string{
"app.kubernetes.io/part-of": "argocd",
},
},
Data: map[string]string{"cluster.inClusterEnabled": "false"},
}
argoCDSecret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: common.ArgoCDSecretName,
Namespace: fakeNamespace,
Labels: map[string]string{
"app.kubernetes.io/part-of": "argocd",
},
},
Data: map[string][]byte{
"admin.password": nil,
"server.secretkey": nil,
},
}
kubeclientset := fake.NewClientset(argoCDConfigMapWithInClusterServerAddressDisabled, argoCDSecret)
settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace)
db := NewDB(fakeNamespace, settingsManager, kubeclientset)
completed := runWatchTest(t, db, []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster){
func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) {
assert.Fail(t, "The in-cluster should not be added when disabled")
},
})
assert.False(t, completed, "Expecting the method to never complete because no cluster is ever added")
}
Loading
Loading