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

Merged
merged 27 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 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
f90a1bc
Merge branch 'master' into fix-in-cluster-disable
agaudreault Jan 22, 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
50 changes: 40 additions & 10 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,39 @@ import (
"github.com/argoproj/argo-cd/v2/util/settings"
)

// getDefaultTestClientSet creates a Clientset with the default argo objects
// and objects specified in parameters
func getDefaultTestClientSet(obj ...runtime.Object) *kubefake.Clientset {
argoCDSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: argocommon.ArgoCDSecretName,
Namespace: "argocd",
Labels: map[string]string{
"app.kubernetes.io/part-of": "argocd",
},
},
Data: map[string][]byte{
"admin.password": nil,
"server.secretkey": nil,
},
}

emptyArgoCDConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: argocommon.ArgoCDConfigMapName,
Namespace: "argocd",
Labels: map[string]string{
"app.kubernetes.io/part-of": "argocd",
},
},
Data: map[string]string{},
}

objects := append(obj, emptyArgoCDConfigMap, argoCDSecret)
kubeclientset := kubefake.NewClientset(objects...)
return kubeclientset
}

func TestCreateOrUpdateInCluster(t *testing.T) {
scheme := runtime.NewScheme()
err := v1alpha1.AddToScheme(scheme)
Expand Down Expand Up @@ -1320,8 +1353,8 @@ func TestRemoveFinalizerOnInvalidDestination_DestinationTypes(t *testing.T) {
},
}

objects := append([]runtime.Object{}, secret)
kubeclientset := kubefake.NewSimpleClientset(objects...)
kubeclientset := getDefaultTestClientSet(secret)

metrics := appsetmetrics.NewFakeAppsetMetrics(client)

argodb := db.NewDB("argocd", settings.NewSettingsManager(context.TODO(), kubeclientset, "argocd"), kubeclientset)
Expand Down Expand Up @@ -2057,8 +2090,7 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
}

objects := append([]runtime.Object{}, secret)
kubeclientset := kubefake.NewSimpleClientset(objects...)
kubeclientset := getDefaultTestClientSet(secret)

argodb := db.NewDB("argocd", settings.NewSettingsManager(context.TODO(), kubeclientset, "argocd"), kubeclientset)

Expand Down Expand Up @@ -2120,7 +2152,7 @@ func TestReconcilerValidationProjectErrorBehaviour(t *testing.T) {
},
}

kubeclientset := kubefake.NewSimpleClientset()
kubeclientset := getDefaultTestClientSet()

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&appSet, &project).WithStatusSubresource(&appSet).WithIndex(&v1alpha1.Application{}, ".metadata.controller", appControllerIndexer).Build()
metrics := appsetmetrics.NewFakeAppsetMetrics(client)
Expand Down Expand Up @@ -2407,8 +2439,7 @@ func applicationsUpdateSyncPolicyTest(t *testing.T, applicationsSyncPolicy v1alp
},
}

objects := append([]runtime.Object{}, secret)
kubeclientset := kubefake.NewSimpleClientset(objects...)
kubeclientset := getDefaultTestClientSet(secret)

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&appSet, &defaultProject).WithStatusSubresource(&appSet).WithIndex(&v1alpha1.Application{}, ".metadata.controller", appControllerIndexer).Build()
metrics := appsetmetrics.NewFakeAppsetMetrics(client)
Expand Down Expand Up @@ -2583,8 +2614,7 @@ func applicationsDeleteSyncPolicyTest(t *testing.T, applicationsSyncPolicy v1alp
},
}

objects := append([]runtime.Object{}, secret)
kubeclientset := kubefake.NewSimpleClientset(objects...)
kubeclientset := getDefaultTestClientSet(secret)

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&appSet, &defaultProject).WithStatusSubresource(&appSet).WithIndex(&v1alpha1.Application{}, ".metadata.controller", appControllerIndexer).Build()
metrics := appsetmetrics.NewFakeAppsetMetrics(client)
Expand Down Expand Up @@ -2703,7 +2733,7 @@ func TestPolicies(t *testing.T) {
Spec: v1alpha1.AppProjectSpec{SourceRepos: []string{"*"}, Destinations: []v1alpha1.ApplicationDestination{{Namespace: "*", Server: "https://kubernetes.default.svc"}}},
}

kubeclientset := kubefake.NewSimpleClientset()
kubeclientset := getDefaultTestClientSet()

for _, c := range []struct {
name string
Expand Down
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 fmt.Errorf("could not get local cluster: %w", err)
}
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 *corev1.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) (*corev1.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].(*corev1.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].(*corev1.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
Loading
Loading