forked from kcp-dev/init-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_test.go
More file actions
200 lines (164 loc) · 6.33 KB
/
basic_test.go
File metadata and controls
200 lines (164 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//go:build e2e
/*
Copyright 2026 The kcp Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clusterinit
import (
"strings"
"testing"
"github.com/go-logr/logr"
initializationv1alpha1 "github.com/kcp-dev/init-agent/sdk/apis/initialization/v1alpha1"
"github.com/kcp-dev/init-agent/test/utils"
"github.com/kcp-dev/logicalcluster/v3"
kcptenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrlruntime "sigs.k8s.io/controller-runtime"
)
var (
rootCluster = logicalcluster.NewPath("root")
)
func TestInitializeNewCluster(t *testing.T) {
const (
targetWorkspace = "my-new-workspace" // the workspace to initialize
initAgentWorkspace = "my-init-agent" // workspace that contains InitTargets/Templates
wstWorkspace = "workspace-types-here" // workspace that contains the WorkspaceType
)
ctx := t.Context()
ctrlruntime.SetLogger(logr.Discard())
// create dummy workspace and WST in it
t.Log("Creating WorkspaceType…")
kcpClusterClient := utils.GetKcpAdminClusterClient(t)
rootClient := kcpClusterClient.Cluster(rootCluster)
wstCluster := utils.CreateAndWaitForWorkspace(t, ctx, rootClient, wstWorkspace)
wstClient := kcpClusterClient.Cluster(wstCluster.Path())
wst := &kcptenancyv1alpha1.WorkspaceType{
ObjectMeta: metav1.ObjectMeta{
Name: "my-workspace-type",
},
Spec: kcptenancyv1alpha1.WorkspaceTypeSpec{
Initializer: true,
},
}
if err := wstClient.Create(ctx, wst); err != nil {
t.Fatalf("Failed to create WorkspaceType: %v", err)
}
utils.GrantWorkspaceAccess(t, ctx, wstClient, utils.Subject(), rbacv1.PolicyRule{
APIGroups: []string{"tenancy.kcp.io"},
Resources: []string{"workspacetypes"},
Verbs: []string{"list", "watch"},
}, rbacv1.PolicyRule{
// we could also grant permissions to initialize for all WorkspaceTypes, but this
// more strict for the test
APIGroups: []string{"tenancy.kcp.io"},
Resources: []string{"workspacetypes"},
ResourceNames: []string{wst.Name},
Verbs: []string{"get", "initialize"},
})
// create init-agent ws
t.Log("Creating init-agent workspace…")
initAgentCluster := utils.CreateAndWaitForWorkspace(t, ctx, rootClient, initAgentWorkspace)
initAgentClient := kcpClusterClient.Cluster(initAgentCluster.Path())
utils.GrantWorkspaceAccess(t, ctx, initAgentClient, utils.Subject(), rbacv1.PolicyRule{
APIGroups: []string{"initialization.kcp.io"},
Resources: []string{"inittargets", "inittemplates"},
Verbs: []string{"get", "list", "watch"},
})
// install CRDs there
t.Log("Installing CRDs…")
utils.ApplyCRD(t, ctx, initAgentClient, "deploy/crd/kcp.io/initialization.kcp.io_inittargets.yaml")
utils.ApplyCRD(t, ctx, initAgentClient, "deploy/crd/kcp.io/initialization.kcp.io_inittemplates.yaml")
// create InitTarget and InitTemplates
t.Logf("Creating init-agent configuration…")
initTemplate := &initializationv1alpha1.InitTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "my-template",
},
Spec: initializationv1alpha1.InitTemplateSpec{
Template: strings.TrimSpace(`
apiVersion: v1
kind: ConfigMap
metadata:
namespace: foobar
name: info
data:
cluster: "{{ .ClusterName }}"
workspace: "{{ .ClusterPath }}"
---
apiVersion: v1
kind: Namespace
metadata:
name: foobar
`),
},
}
if err := initAgentClient.Create(ctx, initTemplate); err != nil {
t.Fatalf("Failed to create InitTemplate: %v", err)
}
initTarget := &initializationv1alpha1.InitTarget{
ObjectMeta: metav1.ObjectMeta{
Name: "init-my-workspace-type",
},
Spec: initializationv1alpha1.InitTargetSpec{
WorkspaceTypeReference: initializationv1alpha1.WorkspaceTypeReference{
Path: rootCluster.Join(wstWorkspace).String(),
Name: wst.Name,
},
Sources: []initializationv1alpha1.InitSource{
{
Template: &initializationv1alpha1.TemplateInitSource{
Name: initTemplate.Name,
},
},
},
},
}
if err := initAgentClient.Create(ctx, initTarget); err != nil {
t.Fatalf("Failed to create InitTarget: %v", err)
}
// start agent
agentKubeconfig := utils.CreateKcpAgentKubeconfig(t, "") // no need to give a path, the agent will auto-update it
utils.RunAgent(ctx, t, agentKubeconfig, rootCluster.Join(initAgentWorkspace).String(), "")
// create final target workspace using that WST from the earlier step
targetWs := &kcptenancyv1alpha1.Workspace{
ObjectMeta: metav1.ObjectMeta{
Name: targetWorkspace,
},
Spec: kcptenancyv1alpha1.WorkspaceSpec{
Type: &kcptenancyv1alpha1.WorkspaceTypeReference{
Path: rootCluster.Join(wstWorkspace).String(),
Name: kcptenancyv1alpha1.WorkspaceTypeName(wst.Name),
},
},
}
t.Logf("Creating workspace %s…", targetWorkspace)
if err := rootClient.Create(ctx, targetWs); err != nil {
t.Fatalf("Failed to create %q workspace: %v", targetWorkspace, err)
}
// wait for the agent to do its work and initialize the cluster and ultimately remove the initializer
targetWs = utils.WaitForWorkspaceInitialization(t, ctx, kcpClusterClient, rootCluster, targetWorkspace)
// connect into the new workspace and verify the generated content
targetClient := kcpClusterClient.Cluster(rootCluster.Join(targetWorkspace))
cm := &corev1.ConfigMap{}
key := types.NamespacedName{Namespace: "foobar", Name: "info"} // as per the template in the InitTemplate
if err := targetClient.Get(ctx, key, cm); err != nil {
t.Fatalf("Failed to find ConfigMap in target workspace: %v", err)
}
if key, expected := "workspace", rootCluster.Join(targetWorkspace).String(); cm.Data[key] != expected {
t.Errorf("Expected ConfigMap to contain %q in the %q key, but got %q.", expected, key, cm.Data[key])
}
if key, expected := "cluster", targetWs.Spec.Cluster; cm.Data[key] != expected {
t.Errorf("Expected ConfigMap to contain %q in the %q key, but got %q.", expected, key, cm.Data[key])
}
}