From e728a57994a2247cbf8b4ee7d4229b3a492641e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20R=C3=BCegg?= Date: Wed, 13 May 2020 20:09:38 +0200 Subject: [PATCH] Set lieutenant-instance fact for new clusters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Simon Rüegg --- deploy/deployment.yaml | 4 ++++ pkg/service/cluster.go | 15 ++++++++++++ pkg/service/cluster_test.go | 46 +++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/deploy/deployment.yaml b/deploy/deployment.yaml index 21fc669..c29999f 100644 --- a/deploy/deployment.yaml +++ b/deploy/deployment.yaml @@ -50,5 +50,9 @@ spec: valueFrom: fieldRef: fieldPath: metadata.namespace + - name: LIEUTENANT_INSTANCE + valueFrom: + fieldRef: + fieldPath: metadata.namespace - name: STEWARD_IMAGE value: docker.io/projectsyn/steward:latest diff --git a/pkg/service/cluster.go b/pkg/service/cluster.go index ff8dd23..45c7570 100644 --- a/pkg/service/cluster.go +++ b/pkg/service/cluster.go @@ -3,6 +3,7 @@ package service import ( "encoding/json" "net/http" + "os" "strings" "github.com/labstack/echo/v4" @@ -12,6 +13,15 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +const ( + // LieutenantInstanceFact defines the name of the fact which specifies the Lieutenant instance + // a cluster was created on + LieutenantInstanceFact = "lieutenant-instance" + + // LieutenantInstanceFactEnvVar is the env var name that's used to get the instance name + LieutenantInstanceFactEnvVar = "LIEUTENANT_INSTANCE" +) + // ListClusters lists all clusters func (s *APIImpl) ListClusters(c echo.Context, params api.ListClustersParams) error { ctx := c.(*APIContext) @@ -46,6 +56,11 @@ func (s *APIImpl) CreateCluster(c echo.Context) error { apiCluster.ClusterId = id cluster := api.NewCRDFromAPICluster(*apiCluster) cluster.Namespace = s.namespace + if cluster.Spec.Facts == nil { + cluster.Spec.Facts = &synv1alpha1.Facts{} + } + (*cluster.Spec.Facts)[LieutenantInstanceFact] = os.Getenv(LieutenantInstanceFactEnvVar) + if err := ctx.client.Create(ctx.context, cluster); err != nil { return err } diff --git a/pkg/service/cluster_test.go b/pkg/service/cluster_test.go index 0989170..dc9c3b6 100644 --- a/pkg/service/cluster_test.go +++ b/pkg/service/cluster_test.go @@ -2,6 +2,7 @@ package service import ( "net/http" + "os" "strings" "testing" @@ -51,12 +52,14 @@ func TestListClusterWrongToken(t *testing.T) { func TestCreateCluster(t *testing.T) { e := setupTest(t) + os.Setenv(LieutenantInstanceFactEnvVar, "") newCluster := api.ClusterProperties{ DisplayName: pointer.ToString("My test cluster"), Tenant: tenantA.Name, Facts: &api.ClusterFacts{ - "cloud": "cloudscale", - "region": "test", + "cloud": "cloudscale", + "region": "test", + LieutenantInstanceFact: "", }, } result := testutil.NewRequest(). @@ -74,6 +77,45 @@ func TestCreateCluster(t *testing.T) { assert.Equal(t, newCluster.Facts, cluster.Facts) } +func TestCreateClusterInstanceFact(t *testing.T) { + e := setupTest(t) + + instanceName := "lieutenant-dev" + os.Setenv(LieutenantInstanceFactEnvVar, instanceName) + newCluster := api.ClusterProperties{ + DisplayName: pointer.ToString("My test cluster"), + Tenant: tenantA.Name, + Facts: &api.ClusterFacts{ + "cloud": "cloudscale", + "region": "test", + }, + } + result := testutil.NewRequest(). + Post("/clusters"). + WithJsonBody(newCluster). + WithHeader(echo.HeaderAuthorization, bearerToken). + Go(t, e) + assert.Equal(t, http.StatusCreated, result.Code()) + cluster := &api.Cluster{} + err := result.UnmarshalJsonToObject(cluster) + assert.NoError(t, err) + assert.NotNil(t, cluster) + assert.Equal(t, instanceName, (*cluster.Facts)[LieutenantInstanceFact]) + + (*newCluster.Facts)[LieutenantInstanceFact] = "test" + result = testutil.NewRequest(). + Post("/clusters"). + WithJsonBody(newCluster). + WithHeader(echo.HeaderAuthorization, bearerToken). + Go(t, e) + assert.Equal(t, http.StatusCreated, result.Code()) + cluster = &api.Cluster{} + err = result.UnmarshalJsonToObject(cluster) + assert.NoError(t, err) + assert.NotNil(t, cluster) + assert.Equal(t, instanceName, (*cluster.Facts)[LieutenantInstanceFact]) +} + func TestCreateClusterNoJSON(t *testing.T) { e := setupTest(t)