Skip to content

Commit b6c5b56

Browse files
authored
Merge pull request #10 from weaveworks/fix-cluster-setup
Fix Required Parameter bug
2 parents 0526fac + 16ff757 commit b6c5b56

File tree

5 files changed

+355
-291
lines changed

5 files changed

+355
-291
lines changed

apis/core/templates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type TemplateParam struct {
6161
// Required indicates whether the param must contain a non-empty value
6262
// +kubebuilder:default:=true
6363
// +optional
64-
Required bool `json:"required,omitempty"`
64+
Required bool `json:"required"`
6565
Options []string `json:"options,omitempty"`
6666

6767
// Default specifies the default value for the parameter
+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
11+
"github.com/google/go-cmp/cmp"
12+
"github.com/google/go-cmp/cmp/cmpopts"
13+
capiv1alpha1 "github.com/weaveworks/templates-controller/apis/capi/v1alpha1"
14+
capiv1alpha2 "github.com/weaveworks/templates-controller/apis/capi/v1alpha2"
15+
"github.com/weaveworks/templates-controller/apis/core"
16+
gitopsv1alpha1 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha1"
17+
gitopsv1alpha2 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha2"
18+
)
19+
20+
func TestCAPITemplateV1Alpha1Conversion(t *testing.T) {
21+
ctx := context.TODO()
22+
23+
ct := &capiv1alpha1.CAPITemplate{
24+
ObjectMeta: metav1.ObjectMeta{
25+
Name: "test-template",
26+
Namespace: "default",
27+
},
28+
TypeMeta: metav1.TypeMeta{
29+
Kind: capiv1alpha1.Kind,
30+
APIVersion: "capi.weave.works/v1alpha1",
31+
},
32+
Spec: core.TemplateSpecV1{
33+
Description: "this is test template 1",
34+
Params: []core.TemplateParam{
35+
{
36+
Name: "CLUSTER_NAME",
37+
Description: "This is used for the cluster naming.",
38+
Required: false,
39+
},
40+
},
41+
},
42+
}
43+
44+
assertNoError(t, testEnv.Create(ctx, ct))
45+
defer cleanupResource(t, testEnv, ct)
46+
47+
updated := &capiv1alpha2.CAPITemplate{}
48+
assertNoError(t, testEnv.Get(ctx, client.ObjectKeyFromObject(ct), updated))
49+
50+
want := &capiv1alpha2.CAPITemplate{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: "test-template",
53+
Namespace: "default",
54+
},
55+
TypeMeta: metav1.TypeMeta{
56+
Kind: capiv1alpha2.Kind,
57+
APIVersion: "capi.weave.works/v1alpha2",
58+
},
59+
Spec: core.TemplateSpec{
60+
Description: "this is test template 1",
61+
RenderType: "envsubst",
62+
Params: []core.TemplateParam{
63+
{
64+
Name: "CLUSTER_NAME",
65+
Description: "This is used for the cluster naming.",
66+
Required: false,
67+
},
68+
},
69+
ResourceTemplates: []core.ResourceTemplate{{}},
70+
},
71+
}
72+
73+
if diff := cmp.Diff(want, updated, ignoreObjectMeta); diff != "" {
74+
t.Fatalf("failed to update the template:\n%s\n", diff)
75+
}
76+
}
77+
78+
func TestGitOpsTemplateV1Alpha1Conversion(t *testing.T) {
79+
ctx := context.TODO()
80+
81+
ct := &gitopsv1alpha1.GitOpsTemplate{
82+
ObjectMeta: metav1.ObjectMeta{
83+
Name: "test-template",
84+
Namespace: "default",
85+
},
86+
TypeMeta: metav1.TypeMeta{
87+
Kind: capiv1alpha1.Kind,
88+
APIVersion: "capi.weave.works/v1alpha1",
89+
},
90+
Spec: core.TemplateSpecV1{
91+
Description: "this is test template 1",
92+
Params: []core.TemplateParam{
93+
{
94+
Name: "CLUSTER_NAME",
95+
Description: "This is used for the cluster naming.",
96+
Required: false,
97+
},
98+
},
99+
},
100+
}
101+
102+
assertNoError(t, testEnv.Create(ctx, ct))
103+
defer cleanupResource(t, testEnv, ct)
104+
105+
updated := &gitopsv1alpha2.GitOpsTemplate{}
106+
assertNoError(t, testEnv.Get(ctx, client.ObjectKeyFromObject(ct), updated))
107+
108+
want := &gitopsv1alpha2.GitOpsTemplate{
109+
ObjectMeta: metav1.ObjectMeta{
110+
Name: "test-template",
111+
Namespace: "default",
112+
},
113+
TypeMeta: metav1.TypeMeta{
114+
Kind: gitopsv1alpha2.Kind,
115+
APIVersion: "templates.weave.works/v1alpha2",
116+
},
117+
Spec: core.TemplateSpec{
118+
Description: "this is test template 1",
119+
RenderType: "envsubst",
120+
Params: []core.TemplateParam{
121+
{
122+
Name: "CLUSTER_NAME",
123+
Description: "This is used for the cluster naming.",
124+
Required: false,
125+
},
126+
},
127+
ResourceTemplates: []core.ResourceTemplate{{}},
128+
},
129+
}
130+
131+
if diff := cmp.Diff(want, updated, ignoreObjectMeta); diff != "" {
132+
t.Fatalf("failed to update the template:\n%s\n", diff)
133+
}
134+
}
135+
136+
var ignoreObjectMeta = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion", "UID", "Generation", "CreationTimestamp", "ManagedFields")
137+
138+
func cleanupResource(t *testing.T, cl client.Client, obj client.Object) {
139+
t.Helper()
140+
if err := cl.Delete(context.TODO(), obj); err != nil {
141+
t.Fatal(err)
142+
}
143+
}
144+
145+
func rawExtension(s string) runtime.RawExtension {
146+
return runtime.RawExtension{
147+
Raw: []byte(s),
148+
}
149+
}
150+
151+
func assertNoError(t *testing.T, err error) {
152+
t.Helper()
153+
if err != nil {
154+
t.Fatal(err)
155+
}
156+
}

controllers/main_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
"time"
10+
11+
"github.com/fluxcd/pkg/runtime/testenv"
12+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
13+
"k8s.io/client-go/kubernetes/scheme"
14+
ctrl "sigs.k8s.io/controller-runtime"
15+
16+
capiv1alpha1 "github.com/weaveworks/templates-controller/apis/capi/v1alpha1"
17+
capiv1alpha2 "github.com/weaveworks/templates-controller/apis/capi/v1alpha2"
18+
gitopsv1alpha1 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha1"
19+
gitopsv1alpha2 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha2"
20+
)
21+
22+
var (
23+
testEnv *testenv.Environment
24+
ctx = ctrl.SetupSignalHandler()
25+
)
26+
27+
func init() {
28+
rand.Seed(time.Now().UnixNano())
29+
}
30+
31+
func TestMain(m *testing.M) {
32+
utilruntime.Must(capiv1alpha1.AddToScheme(scheme.Scheme))
33+
utilruntime.Must(capiv1alpha2.AddToScheme(scheme.Scheme))
34+
utilruntime.Must(gitopsv1alpha1.AddToScheme(scheme.Scheme))
35+
utilruntime.Must(gitopsv1alpha2.AddToScheme(scheme.Scheme))
36+
testEnv = testenv.New(testenv.WithCRDPath(filepath.Join("..", "config", "crd", "bases")))
37+
38+
if err := (&CAPITemplateReconciler{
39+
Client: testEnv,
40+
Scheme: testEnv.GetScheme(),
41+
}).SetupWithManager(testEnv); err != nil {
42+
panic(fmt.Sprintf("Failed to start CAPITemplateReconciler: %v", err))
43+
}
44+
45+
// utilruntime.Must((&capiv1alpha1.CAPITemplate{}).SetupWebhookWithManager(testEnv))
46+
utilruntime.Must((&capiv1alpha2.CAPITemplate{}).SetupWebhookWithManager(testEnv))
47+
48+
go func() {
49+
fmt.Println("Starting the test environment")
50+
if err := testEnv.Start(ctx); err != nil {
51+
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
52+
}
53+
}()
54+
<-testEnv.Manager.Elected()
55+
56+
code := m.Run()
57+
58+
fmt.Println("Stopping the test environment")
59+
if err := testEnv.Stop(); err != nil {
60+
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
61+
}
62+
63+
os.Exit(code)
64+
}

go.mod

+36-46
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,68 @@ module github.com/weaveworks/templates-controller
33
go 1.19
44

55
require (
6-
github.com/stretchr/testify v1.7.0
7-
k8s.io/apimachinery v0.25.0
8-
k8s.io/client-go v0.25.0
9-
sigs.k8s.io/controller-runtime v0.13.0
6+
github.com/fluxcd/pkg/runtime v0.31.0
7+
github.com/stretchr/testify v1.8.2
8+
k8s.io/apimachinery v0.26.2
9+
k8s.io/client-go v0.26.2
10+
sigs.k8s.io/controller-runtime v0.14.5
1011
)
1112

1213
require (
13-
cloud.google.com/go v0.97.0 // indirect
14-
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
15-
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
16-
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
17-
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
18-
github.com/Azure/go-autorest/logger v0.2.1 // indirect
19-
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
20-
github.com/PuerkitoBio/purell v1.1.1 // indirect
21-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
2214
github.com/beorn7/perks v1.0.1 // indirect
2315
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2416
github.com/davecgh/go-spew v1.1.1 // indirect
25-
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
17+
github.com/emicklei/go-restful/v3 v3.10.0 // indirect
2618
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
27-
github.com/fsnotify/fsnotify v1.5.4 // indirect
19+
github.com/fsnotify/fsnotify v1.6.0 // indirect
2820
github.com/go-logr/logr v1.2.3 // indirect
2921
github.com/go-logr/zapr v1.2.3 // indirect
3022
github.com/go-openapi/jsonpointer v0.19.5 // indirect
31-
github.com/go-openapi/jsonreference v0.19.5 // indirect
32-
github.com/go-openapi/swag v0.19.14 // indirect
23+
github.com/go-openapi/jsonreference v0.20.0 // indirect
24+
github.com/go-openapi/swag v0.22.3 // indirect
3325
github.com/gogo/protobuf v1.3.2 // indirect
34-
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
3526
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3627
github.com/golang/protobuf v1.5.2 // indirect
37-
github.com/google/gnostic v0.5.7-v3refs // indirect
38-
github.com/google/go-cmp v0.5.8 // indirect
39-
github.com/google/gofuzz v1.1.0 // indirect
40-
github.com/google/uuid v1.1.2 // indirect
41-
github.com/imdario/mergo v0.3.12 // indirect
28+
github.com/google/gnostic v0.6.9 // indirect
29+
github.com/google/go-cmp v0.5.9 // indirect
30+
github.com/google/gofuzz v1.2.0 // indirect
31+
github.com/google/uuid v1.3.0 // indirect
32+
github.com/imdario/mergo v0.3.13 // indirect
4233
github.com/josharian/intern v1.0.0 // indirect
4334
github.com/json-iterator/go v1.1.12 // indirect
44-
github.com/mailru/easyjson v0.7.6 // indirect
45-
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
35+
github.com/mailru/easyjson v0.7.7 // indirect
36+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
4637
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4738
github.com/modern-go/reflect2 v1.0.2 // indirect
4839
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4940
github.com/pkg/errors v0.9.1 // indirect
5041
github.com/pmezard/go-difflib v1.0.0 // indirect
51-
github.com/prometheus/client_golang v1.12.2 // indirect
52-
github.com/prometheus/client_model v0.2.0 // indirect
53-
github.com/prometheus/common v0.32.1 // indirect
54-
github.com/prometheus/procfs v0.7.3 // indirect
42+
github.com/prometheus/client_golang v1.14.0 // indirect
43+
github.com/prometheus/client_model v0.3.0 // indirect
44+
github.com/prometheus/common v0.37.0 // indirect
45+
github.com/prometheus/procfs v0.8.0 // indirect
5546
github.com/spf13/pflag v1.0.5 // indirect
56-
go.uber.org/atomic v1.7.0 // indirect
57-
go.uber.org/multierr v1.6.0 // indirect
58-
go.uber.org/zap v1.21.0 // indirect
59-
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
60-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
61-
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
62-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
63-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
64-
golang.org/x/text v0.3.7 // indirect
65-
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
47+
go.uber.org/atomic v1.10.0 // indirect
48+
go.uber.org/multierr v1.8.0 // indirect
49+
go.uber.org/zap v1.24.0 // indirect
50+
golang.org/x/net v0.7.0 // indirect
51+
golang.org/x/oauth2 v0.2.0 // indirect
52+
golang.org/x/sys v0.5.0 // indirect
53+
golang.org/x/term v0.5.0 // indirect
54+
golang.org/x/text v0.7.0 // indirect
55+
golang.org/x/time v0.3.0 // indirect
6656
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
6757
google.golang.org/appengine v1.6.7 // indirect
68-
google.golang.org/protobuf v1.28.0 // indirect
58+
google.golang.org/protobuf v1.28.1 // indirect
6959
gopkg.in/inf.v0 v0.9.1 // indirect
7060
gopkg.in/yaml.v2 v2.4.0 // indirect
7161
gopkg.in/yaml.v3 v3.0.1 // indirect
72-
k8s.io/api v0.25.0 // indirect
73-
k8s.io/apiextensions-apiserver v0.25.0 // indirect
74-
k8s.io/component-base v0.25.0 // indirect
75-
k8s.io/klog/v2 v2.70.1 // indirect
76-
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
77-
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
62+
k8s.io/api v0.26.2 // indirect
63+
k8s.io/apiextensions-apiserver v0.26.1 // indirect
64+
k8s.io/component-base v0.26.2 // indirect
65+
k8s.io/klog/v2 v2.90.1 // indirect
66+
k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70 // indirect
67+
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
7868
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
7969
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
8070
sigs.k8s.io/yaml v1.3.0 // indirect

0 commit comments

Comments
 (0)