Skip to content

Commit

Permalink
Set lieutenant-instance fact for new clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Rüegg <simon@rueggs.ch>
  • Loading branch information
srueg committed May 13, 2020
1 parent 46260a9 commit e728a57
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions deploy/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions pkg/service/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"encoding/json"
"net/http"
"os"
"strings"

"github.com/labstack/echo/v4"
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
46 changes: 44 additions & 2 deletions pkg/service/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"net/http"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -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().
Expand All @@ -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)

Expand Down

0 comments on commit e728a57

Please sign in to comment.