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 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3057a28
fix(in-cluster): do not allow the cluster to be used when disabled
agaudreault Dec 16, 2024
eb4a528
typo
agaudreault Dec 17, 2024
ca03d10
linter
agaudreault Dec 17, 2024
6ec28fc
Merge remote-tracking branch 'upstream/master' into fix-in-cluster-di…
agaudreault Dec 17, 2024
0a72dd8
e2e
agaudreault Dec 18, 2024
35d6ec0
fix e2e
agaudreault Dec 18, 2024
c96ecd8
unquote CLI error
agaudreault Dec 18, 2024
224869d
Revert "unquote CLI error"
agaudreault Dec 18, 2024
7fad375
just match the quoted result
agaudreault Dec 18, 2024
d5a0c64
Merge remote-tracking branch 'upstream/master' into fix-in-cluster-di…
agaudreault Dec 18, 2024
223171a
Merge branch 'master' into fix-in-cluster-disable
agaudreault Dec 19, 2024
ac39de3
Merge branch 'master' into fix-in-cluster-disable
agaudreault Jan 3, 2025
3e71bac
Merge remote-tracking branch 'upstream/master' into fix-in-cluster-di…
agaudreault Jan 3, 2025
92321c2
fix merge
agaudreault Jan 3, 2025
6fe112d
fix some tests
agaudreault Jan 3, 2025
0b8adef
lint
agaudreault Jan 3, 2025
929b23d
linter
agaudreault Jan 3, 2025
9bd1f1f
code review
agaudreault Jan 6, 2025
36a1971
Merge branch 'master' into fix-in-cluster-disable
agaudreault Jan 8, 2025
14dc2d2
Merge remote-tracking branch 'upstream/master' into fix-in-cluster-di…
agaudreault Jan 17, 2025
1d96b84
add migration docs
agaudreault Jan 17, 2025
7819589
fix tests
agaudreault Jan 17, 2025
259dda4
fix tests
agaudreault Jan 17, 2025
da9bc98
Merge remote-tracking branch 'upstream/master' into fix-in-cluster-di…
agaudreault Jan 17, 2025
8e1bd59
lint
agaudreault Jan 17, 2025
815dff5
fix test
agaudreault Jan 17, 2025
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
59 changes: 47 additions & 12 deletions test/e2e/app_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,28 @@ func TestComparisonFailsIfClusterNotAdded(t *testing.T) {
Expect(DoesNotExist())
}

func TestComparisonFailsIfInClusterDisabled(t *testing.T) {
Given(t).
Path(guestbookPath).
DestServer(KubernetesInternalAPIServerAddr).
When().
CreateApp().
Refresh(RefreshTypeNormal).
Sync().
Then().
Expect(Success("")).
Expect(HealthIs(health.HealthStatusHealthy)).
Expect(SyncStatusIs(SyncStatusCodeSynced)).
When().
SetParamInSettingConfigMap("cluster.inClusterEnabled", "false").
Refresh(RefreshTypeNormal).
Then().
Expect(Success("")).
Expect(HealthIs(health.HealthStatusUnknown)).
Expect(SyncStatusIs(SyncStatusCodeUnknown)).
Expect(Condition(ApplicationConditionInvalidSpecError, fmt.Sprintf("cluster %q is disabled", KubernetesInternalAPIServerAddr)))
}

func TestCannotSetInvalidPath(t *testing.T) {
Given(t).
Path(guestbookPath).
Expand Down Expand Up @@ -1117,34 +1139,34 @@ definitions:
end
return _copy(object)
end

job = {}
job.apiVersion = "batch/v1"
job.kind = "Job"

job.metadata = {}
job.metadata.name = obj.metadata.name .. "-123"
job.metadata.namespace = obj.metadata.namespace

ownerRef = {}
ownerRef.apiVersion = obj.apiVersion
ownerRef.kind = obj.kind
ownerRef.name = obj.metadata.name
ownerRef.uid = obj.metadata.uid
job.metadata.ownerReferences = {}
job.metadata.ownerReferences[1] = ownerRef

job.spec = {}
job.spec.suspend = false
job.spec.template = {}
job.spec.template.spec = deepCopy(obj.spec.jobTemplate.spec.template.spec)

impactedResource = {}
impactedResource.operation = "create"
impactedResource.resource = job
result = {}
result[1] = impactedResource

return result`

func TestNewStyleResourceActionPermitted(t *testing.T) {
Expand Down Expand Up @@ -1221,28 +1243,28 @@ definitions:
end
return _copy(object)
end

job = {}
job.apiVersion = "batch/v1"
job.kind = "Job"

job.metadata = {}
job.metadata.name = obj.metadata.name .. "-123"
job.metadata.namespace = obj.metadata.namespace

ownerRef = {}
ownerRef.apiVersion = obj.apiVersion
ownerRef.kind = obj.kind
ownerRef.name = obj.metadata.name
ownerRef.uid = obj.metadata.uid
job.metadata.ownerReferences = {}
job.metadata.ownerReferences[1] = ownerRef

job.spec = {}
job.spec.suspend = false
job.spec.template = {}
job.spec.template.spec = deepCopy(obj.spec.jobTemplate.spec.template.spec)

impactedResource1 = {}
impactedResource1.operation = "create"
impactedResource1.resource = job
Expand All @@ -1255,7 +1277,7 @@ definitions:
impactedResource2.resource = obj

result[2] = impactedResource2

return result`

func TestNewStyleResourceActionMixedOk(t *testing.T) {
Expand Down Expand Up @@ -2160,6 +2182,19 @@ func TestCreateAppWithNoNameSpaceWhenRequired2(t *testing.T) {
})
}

func TestCreateAppWithInClusterDisabled(t *testing.T) {
Given(t).
Path(guestbookPath).
DestServer(KubernetesInternalAPIServerAddr).
When().
SetParamInSettingConfigMap("cluster.inClusterEnabled", "false").
IgnoreErrors().
CreateApp().
Then().
// RPC error messages are quoted: time="2024-12-18T04:13:58Z" level=fatal msg="<Quoted value>"
Expect(Error("", fmt.Sprintf(`cluster \"%s\" is disabled`, KubernetesInternalAPIServerAddr)))
}

func TestListResource(t *testing.T) {
SkipOnEnv(t, "OPENSHIFT")
Given(t).
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/fixture/app/expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func Error(message, err string, matchers ...func(string, string) bool) Expectati
return failed, fmt.Sprintf("output does not contain '%s'", message)
}
if !match(c.actions.lastError.Error(), err) {
return failed, fmt.Sprintf("error does not contain '%s'", message)
return failed, fmt.Sprintf("error does not contain '%s'", err)
}
return succeeded, fmt.Sprintf("error '%s'", message)
}
Expand Down
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
agaudreault marked this conversation as resolved.
Show resolved Hide resolved
}
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