Skip to content

Commit

Permalink
Merge pull request #10 from weaveworks/fix-cluster-setup
Browse files Browse the repository at this point in the history
Fix Required Parameter bug
  • Loading branch information
bigkevmcd authored Mar 13, 2023
2 parents 0526fac + 16ff757 commit b6c5b56
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 291 deletions.
2 changes: 1 addition & 1 deletion apis/core/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type TemplateParam struct {
// Required indicates whether the param must contain a non-empty value
// +kubebuilder:default:=true
// +optional
Required bool `json:"required,omitempty"`
Required bool `json:"required"`
Options []string `json:"options,omitempty"`

// Default specifies the default value for the parameter
Expand Down
156 changes: 156 additions & 0 deletions controllers/capitemplate_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package controllers

import (
"context"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
capiv1alpha1 "github.com/weaveworks/templates-controller/apis/capi/v1alpha1"
capiv1alpha2 "github.com/weaveworks/templates-controller/apis/capi/v1alpha2"
"github.com/weaveworks/templates-controller/apis/core"
gitopsv1alpha1 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha1"
gitopsv1alpha2 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha2"
)

func TestCAPITemplateV1Alpha1Conversion(t *testing.T) {
ctx := context.TODO()

ct := &capiv1alpha1.CAPITemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "test-template",
Namespace: "default",
},
TypeMeta: metav1.TypeMeta{
Kind: capiv1alpha1.Kind,
APIVersion: "capi.weave.works/v1alpha1",
},
Spec: core.TemplateSpecV1{
Description: "this is test template 1",
Params: []core.TemplateParam{
{
Name: "CLUSTER_NAME",
Description: "This is used for the cluster naming.",
Required: false,
},
},
},
}

assertNoError(t, testEnv.Create(ctx, ct))
defer cleanupResource(t, testEnv, ct)

updated := &capiv1alpha2.CAPITemplate{}
assertNoError(t, testEnv.Get(ctx, client.ObjectKeyFromObject(ct), updated))

want := &capiv1alpha2.CAPITemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "test-template",
Namespace: "default",
},
TypeMeta: metav1.TypeMeta{
Kind: capiv1alpha2.Kind,
APIVersion: "capi.weave.works/v1alpha2",
},
Spec: core.TemplateSpec{
Description: "this is test template 1",
RenderType: "envsubst",
Params: []core.TemplateParam{
{
Name: "CLUSTER_NAME",
Description: "This is used for the cluster naming.",
Required: false,
},
},
ResourceTemplates: []core.ResourceTemplate{{}},
},
}

if diff := cmp.Diff(want, updated, ignoreObjectMeta); diff != "" {
t.Fatalf("failed to update the template:\n%s\n", diff)
}
}

func TestGitOpsTemplateV1Alpha1Conversion(t *testing.T) {
ctx := context.TODO()

ct := &gitopsv1alpha1.GitOpsTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "test-template",
Namespace: "default",
},
TypeMeta: metav1.TypeMeta{
Kind: capiv1alpha1.Kind,
APIVersion: "capi.weave.works/v1alpha1",
},
Spec: core.TemplateSpecV1{
Description: "this is test template 1",
Params: []core.TemplateParam{
{
Name: "CLUSTER_NAME",
Description: "This is used for the cluster naming.",
Required: false,
},
},
},
}

assertNoError(t, testEnv.Create(ctx, ct))
defer cleanupResource(t, testEnv, ct)

updated := &gitopsv1alpha2.GitOpsTemplate{}
assertNoError(t, testEnv.Get(ctx, client.ObjectKeyFromObject(ct), updated))

want := &gitopsv1alpha2.GitOpsTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "test-template",
Namespace: "default",
},
TypeMeta: metav1.TypeMeta{
Kind: gitopsv1alpha2.Kind,
APIVersion: "templates.weave.works/v1alpha2",
},
Spec: core.TemplateSpec{
Description: "this is test template 1",
RenderType: "envsubst",
Params: []core.TemplateParam{
{
Name: "CLUSTER_NAME",
Description: "This is used for the cluster naming.",
Required: false,
},
},
ResourceTemplates: []core.ResourceTemplate{{}},
},
}

if diff := cmp.Diff(want, updated, ignoreObjectMeta); diff != "" {
t.Fatalf("failed to update the template:\n%s\n", diff)
}
}

var ignoreObjectMeta = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion", "UID", "Generation", "CreationTimestamp", "ManagedFields")

func cleanupResource(t *testing.T, cl client.Client, obj client.Object) {
t.Helper()
if err := cl.Delete(context.TODO(), obj); err != nil {
t.Fatal(err)
}
}

func rawExtension(s string) runtime.RawExtension {
return runtime.RawExtension{
Raw: []byte(s),
}
}

func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
64 changes: 64 additions & 0 deletions controllers/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package controllers

import (
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"

"github.com/fluxcd/pkg/runtime/testenv"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"

capiv1alpha1 "github.com/weaveworks/templates-controller/apis/capi/v1alpha1"
capiv1alpha2 "github.com/weaveworks/templates-controller/apis/capi/v1alpha2"
gitopsv1alpha1 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha1"
gitopsv1alpha2 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha2"
)

var (
testEnv *testenv.Environment
ctx = ctrl.SetupSignalHandler()
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func TestMain(m *testing.M) {
utilruntime.Must(capiv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(capiv1alpha2.AddToScheme(scheme.Scheme))
utilruntime.Must(gitopsv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(gitopsv1alpha2.AddToScheme(scheme.Scheme))
testEnv = testenv.New(testenv.WithCRDPath(filepath.Join("..", "config", "crd", "bases")))

if err := (&CAPITemplateReconciler{
Client: testEnv,
Scheme: testEnv.GetScheme(),
}).SetupWithManager(testEnv); err != nil {
panic(fmt.Sprintf("Failed to start CAPITemplateReconciler: %v", err))
}

// utilruntime.Must((&capiv1alpha1.CAPITemplate{}).SetupWebhookWithManager(testEnv))
utilruntime.Must((&capiv1alpha2.CAPITemplate{}).SetupWebhookWithManager(testEnv))

go func() {
fmt.Println("Starting the test environment")
if err := testEnv.Start(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
}
}()
<-testEnv.Manager.Elected()

code := m.Run()

fmt.Println("Stopping the test environment")
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
}

os.Exit(code)
}
82 changes: 36 additions & 46 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,68 @@ module github.com/weaveworks/templates-controller
go 1.19

require (
github.com/stretchr/testify v1.7.0
k8s.io/apimachinery v0.25.0
k8s.io/client-go v0.25.0
sigs.k8s.io/controller-runtime v0.13.0
github.com/fluxcd/pkg/runtime v0.31.0
github.com/stretchr/testify v1.8.2
k8s.io/apimachinery v0.26.2
k8s.io/client-go v0.26.2
sigs.k8s.io/controller-runtime v0.14.5
)

require (
cloud.google.com/go v0.97.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.2.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.25.0 // indirect
k8s.io/apiextensions-apiserver v0.25.0 // indirect
k8s.io/component-base v0.25.0 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
k8s.io/api v0.26.2 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.2 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading

0 comments on commit b6c5b56

Please sign in to comment.