Skip to content

Commit

Permalink
Merge pull request #157 from projectsyn/feature/dynamic-facts
Browse files Browse the repository at this point in the history
Add dynamic facts
  • Loading branch information
glrf authored Aug 23, 2021
2 parents 1ce8d16 + 6cade04 commit dcea9f3
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 88 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ ANTORA_PREVIEW_CMD ?= $(DOCKER_CMD) run --rm --publish 35729:35729 --publish 202
VALE_CMD ?= $(DOCKER_CMD) run $(DOCKER_ARGS) --volume "$${PWD}"/docs/modules:/pages vshn/vale:2.6.1
VALE_ARGS ?= --minAlertLevel=error --config=/pages/ROOT/pages/.vale.ini /pages

SWAGGER_CMD ?= $(DOCKER_CMD) run --rm --user "$$(id -u)" --volume "$${PWD}:/src" -p 8080:8080 -e SWAGGER_JSON=/src/openapi.yaml swaggerapi/swagger-ui

openapi_generator_img ?= docker.io/openapitools/openapi-generator:cli-v4.3.0
openapi_validate_cmd ?= $(DOCKER_CMD) run $(DOCKER_ARGS) --volume "$${PWD}"/openapi.yaml:/openapi.yaml $(openapi_generator_img) \
validate -i /openapi.yaml
Expand Down Expand Up @@ -101,3 +103,7 @@ docs-serve:
.PHONY: docs-vale
docs-vale:
$(VALE_CMD) $(VALE_ARGS)

.PHONY: docs-vale
serve-api-doc:
$(SWAGGER_CMD)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/getkin/kin-openapi v0.61.0
github.com/hashicorp/golang-lru v0.5.4
github.com/labstack/echo/v4 v4.3.0
github.com/projectsyn/lieutenant-operator v1.0.0
github.com/projectsyn/lieutenant-operator v1.1.0
github.com/stretchr/testify v1.7.0
github.com/taion809/haikunator v0.0.0-20150324135039-4e414e676fd1
k8s.io/api v0.21.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/projectsyn/lieutenant-operator v1.0.0 h1:8J4WWnbrEyKEBWmUr8P/0SXiBf9dzp26714i5xt7UXY=
github.com/projectsyn/lieutenant-operator v1.0.0/go.mod h1:JY1hQUr8JNlU8PqjOoD0bkvpHmQOaMT72Ffy+Ri4A54=
github.com/projectsyn/lieutenant-operator v1.1.0 h1:9DyHpP9vjN0+8ZsaMTiZIYAec3oZLx8+piDRhMuJ64U=
github.com/projectsyn/lieutenant-operator v1.1.0/go.mod h1:JY1hQUr8JNlU8PqjOoD0bkvpHmQOaMT72Ffy+Ri4A54=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
Expand Down
16 changes: 16 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ components:
example: My very important cluster
facts:
$ref: '#/components/schemas/ClusterFacts'
dynamicFacts:
$ref: '#/components/schemas/DynamicClusterFacts'
gitRepo:
$ref: '#/components/schemas/GitRepo'
tenantGitRepoRevision:
Expand Down Expand Up @@ -129,6 +131,20 @@ components:
distribution: openshift4
cloud: aws
region: eu-west-1
DynamicClusterFacts:
type: object
description: Dynamic facts about a cluster object. Are periodically udpated by Project Syn and should not be set manually.
example:
kubernetesVersion:
buildDate: "2021-04-16T19:04:44Z"
compiler: gc
gitCommit: 8d0432824a9fd9474b67138b7630c33f285d332f
gitTreeState: clean
gitVersion: v1.20.6+k3s1
goVersion: go1.15.10
major: "1"
minor: "20"
platform: linux/amd64
Cluster:
allOf:
- $ref: '#/components/schemas/ClusterId'
Expand Down
132 changes: 71 additions & 61 deletions pkg/api/openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 39 additions & 5 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -200,6 +201,14 @@ func NewAPIClusterFromCRD(cluster synv1alpha1.Cluster) *Cluster {
apiCluster.Facts = &facts
}

if cluster.Status.Facts != nil {
facts := DynamicClusterFacts{}
for key, value := range cluster.Status.Facts {
facts[key] = unmarshalFact(value)
}
apiCluster.DynamicFacts = &facts
}

if cluster.Spec.GitRepoTemplate != nil {
if stewardKey, ok := cluster.Spec.GitRepoTemplate.DeployKeys["steward"]; ok {
sshKey := fmt.Sprintf("%s %s", stewardKey.Type, stewardKey.Key)
Expand All @@ -214,8 +223,19 @@ func NewAPIClusterFromCRD(cluster synv1alpha1.Cluster) *Cluster {
return apiCluster
}

func unmarshalFact(fact string) interface{} {
var intFact interface{}
err := json.Unmarshal([]byte(fact), &intFact)
if err != nil {
// The given string is not a JSON value
// Fall back to returning the raw string
return fact
}
return intFact
}

// NewCRDFromAPICluster transforms an API cluster into the CRD representation
func NewCRDFromAPICluster(apiCluster Cluster) *synv1alpha1.Cluster {
func NewCRDFromAPICluster(apiCluster Cluster) (*synv1alpha1.Cluster, error) {
cluster := &synv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: string(apiCluster.ClusterId.Id),
Expand All @@ -234,12 +254,11 @@ func NewCRDFromAPICluster(apiCluster Cluster) *synv1alpha1.Cluster {
cluster.Spec.GitHostKeys = *apiCluster.GitRepo.HostKeys
}

SyncCRDFromAPICluster(apiCluster.ClusterProperties, cluster)

return cluster
err := SyncCRDFromAPICluster(apiCluster.ClusterProperties, cluster)
return cluster, err
}

func SyncCRDFromAPICluster(source ClusterProperties, target *synv1alpha1.Cluster) {
func SyncCRDFromAPICluster(source ClusterProperties, target *synv1alpha1.Cluster) error {
if source.Annotations != nil {
if target.Annotations == nil {
target.Annotations = map[string]string{}
Expand Down Expand Up @@ -296,6 +315,21 @@ func SyncCRDFromAPICluster(source ClusterProperties, target *synv1alpha1.Cluster
}
}
}

if source.DynamicFacts != nil {
if target.Status.Facts == nil {
target.Status.Facts = synv1alpha1.Facts{}
}

for key, value := range *source.DynamicFacts {
encodedFact, err := json.Marshal(value)
if err != nil {
return err
}
target.Status.Facts[key] = string(encodedFact)
}
}
return nil
}

func newGitRepoTemplate(repo *GitRepo, name string) *synv1alpha1.GitRepoTemplate {
Expand Down
Loading

0 comments on commit dcea9f3

Please sign in to comment.