Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Verify descriptor apiVersion to be supported by the current cattleclt…
Browse files Browse the repository at this point in the history
… version (#38)
  • Loading branch information
wtschreiter authored Jun 9, 2019
1 parent 64158fa commit 7e3cbab
Show file tree
Hide file tree
Showing 21 changed files with 1,506 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ env:
install: true
script:
- go test -v -mod=vendor ./...
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/cmd.Version=${TRAVIS_TAG:-0.0.0-dev} -d -s -w -extldflags \"-static\"" -a -tags netgo -installsuffix netgo -mod=vendor -o build/linux/cattlectl
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -d -s -w -extldflags \"-static\"" -a -tags netgo -installsuffix netgo -mod=vendor -o build/linux/cattlectl
- tar czpvf build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-linux.tar.gz build/linux/
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/cmd.Version=${TRAVIS_TAG:-0.0.0-dev} -s -w" -a -tags netgo -installsuffix netgo -mod=vendor -o build/darwin/cattlectl
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -s -w" -a -tags netgo -installsuffix netgo -mod=vendor -o build/darwin/cattlectl
- tar czpvf build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-darwin.tar.gz build/darwin/
deploy:
provider: releases
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN go test -mod=vendor -v ./...
#build the binary
RUN echo "Building VERSION=$(git describe --tags)" 1>&2 && \
go build \
-ldflags "-X github.com/bitgrip/cattlectl/cmd.Version=$(git describe --tags) -d -s -w -extldflags \"-static\"" \
-ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=$(git describe --tags) -d -s -w -extldflags \"-static\"" \
-a -tags netgo -installsuffix netgo \
-mod=vendor \
-o /go/bin/cattlectl
Expand Down
5 changes: 2 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ package cmd
import (
"fmt"

"github.com/bitgrip/cattlectl/internal/pkg/ctl"
"github.com/spf13/cobra"
)

var Version = "v0.0.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "version of cattlectl",
Long: `Prints the version of cattlectl to stdout`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(Version)
fmt.Println(ctl.Version)
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bitgrip/cattlectl
require (
cloud.google.com/go v0.39.0 // indirect
github.com/766b/go-outliner v0.0.0-20180511142203-fc6edecdadd7 // indirect
github.com/Masterminds/semver v1.4.2
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/OneOfOne/xxhash v1.2.5 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/766b/go-outliner v0.0.0-20180511142203-fc6edecdadd7/go.mod h1:1SzhTho
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down
39 changes: 32 additions & 7 deletions internal/pkg/ctl/cattle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
yaml "gopkg.in/yaml.v2"
)

// Supported descriptor expected in the field 'kind'
const (
ProjectKind = "Project"
JobKind = "Job"
Expand Down Expand Up @@ -51,11 +52,15 @@ var (
newStatefulSetParser = project.NewStatefulSetParser
)

// ApplyDescriptor the the CTL perform a apply action
func ApplyDescriptor(file string, data []byte, values map[string]interface{}, config config.Config) error {
kind, err := GetKind(data)
apiVersion, kind, err := GetAPIVersionAndKind(data)
if err != nil {
return err
}
if !isSupportedAPIVersion(apiVersion) {
return fmt.Errorf("Unsupported api version %s", apiVersion)
}
switch kind {
case ProjectKind:
project := projectModel.Project{}
Expand Down Expand Up @@ -111,6 +116,7 @@ func ApplyDescriptor(file string, data []byte, values map[string]interface{}, co
return nil
}

// ApplyCronJob the the CTL perform a apply action to a cronjob descriptor
func ApplyCronJob(cronJobDescriptor projectModel.CronJobDescriptor, config config.Config) error {
_, _, projectClient, err := fillWorkloadMetadata(&cronJobDescriptor.Metadata, config)
if err != nil {
Expand All @@ -123,6 +129,7 @@ func ApplyCronJob(cronJobDescriptor projectModel.CronJobDescriptor, config confi
return converger.Converge()
}

// ApplyJob the the CTL perform a apply action to a job descriptor
func ApplyJob(jobDescriptor projectModel.JobDescriptor, config config.Config) error {
_, _, projectClient, err := fillWorkloadMetadata(&jobDescriptor.Metadata, config)
if err != nil {
Expand All @@ -135,6 +142,7 @@ func ApplyJob(jobDescriptor projectModel.JobDescriptor, config config.Config) er
return converger.Converge()
}

// ApplyDeployment the the CTL perform a apply action to a deployment descriptor
func ApplyDeployment(deploymentDescriptor projectModel.DeploymentDescriptor, config config.Config) error {
_, _, projectClient, err := fillWorkloadMetadata(&deploymentDescriptor.Metadata, config)
if err != nil {
Expand All @@ -147,6 +155,7 @@ func ApplyDeployment(deploymentDescriptor projectModel.DeploymentDescriptor, con
return converger.Converge()
}

// ApplyDaemonSet the the CTL perform a apply action to a daemon set descriptor
func ApplyDaemonSet(daemonSetDescriptor projectModel.DaemonSetDescriptor, config config.Config) error {
_, _, projectClient, err := fillWorkloadMetadata(&daemonSetDescriptor.Metadata, config)
if err != nil {
Expand All @@ -159,6 +168,7 @@ func ApplyDaemonSet(daemonSetDescriptor projectModel.DaemonSetDescriptor, config
return converger.Converge()
}

// ApplyStatefulSet the the CTL perform a apply action to a stateful set descriptor
func ApplyStatefulSet(statefulSetDescriptor projectModel.StatefulSetDescriptor, config config.Config) error {
_, _, projectClient, err := fillWorkloadMetadata(&statefulSetDescriptor.Metadata, config)
if err != nil {
Expand All @@ -171,6 +181,7 @@ func ApplyStatefulSet(statefulSetDescriptor projectModel.StatefulSetDescriptor,
return converger.Converge()
}

// ApplyProject the the CTL perform a apply action to a project descriptor
func ApplyProject(project projectModel.Project, config config.Config) error {
_, clusterClient, err := fillProjectMetadata(&project.Metadata, config)
if err != nil {
Expand All @@ -183,23 +194,37 @@ func ApplyProject(project projectModel.Project, config config.Config) error {
return converger.Converge()
}

func GetKind(data []byte) (string, error) {
// GetAPIVersionAndKind reads API version and kind from the data
func GetAPIVersionAndKind(data []byte) (string, string, error) {
structure := make(map[string]interface{})
apiVersion := "UNKNOWN"
kind := "UNKNOWN"
if err := yaml.Unmarshal(data, &structure); err != nil {
return "", err
return apiVersion, kind, err
}
if foundKind, exists := structure["kind"]; exists {
kind = fmt.Sprint(foundKind)
} else {
return apiVersion, kind, fmt.Errorf("Kind is undefined")
}
if kind, exists := structure["kind"]; exists {
return fmt.Sprint(kind), nil
if foundAPIVersion, exists := structure["api_version"]; exists {
apiVersion = fmt.Sprint(foundAPIVersion)
} else {
return apiVersion, kind, fmt.Errorf("Kind is undefined")
}

return "UNKNOWN", fmt.Errorf("Kind is undefined")
return apiVersion, kind, nil
}

// ParseAndPrintDescriptor parse and print the 'data'
func ParseAndPrintDescriptor(file string, data []byte, values map[string]interface{}, config config.Config) error {
kind, err := GetKind(data)
apiVersion, kind, err := GetAPIVersionAndKind(data)
if err != nil {
return err
}
if !isSupportedAPIVersion(apiVersion) {
return fmt.Errorf("Unsupported api version %s", apiVersion)
}
var descriptor interface{}
switch kind {
case ProjectKind:
Expand Down
41 changes: 41 additions & 0 deletions internal/pkg/ctl/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2018 Bitgrip <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ctl

import (
"fmt"

"github.com/Masterminds/semver"
)

// Version is the current build version
var Version = "v1.2.0-local"

func isSupportedAPIVersion(apiVersion string) bool {
v, err := semver.NewVersion(Version)
if err != nil {
return false
}
c, err := semver.NewConstraint(fmt.Sprintf("<= %v.%v", v.Major(), v.Minor()))
if err != nil {
return false
}

av, err := semver.NewVersion(apiVersion)
if err != nil {
return false
}
return c.Check(av)
}
56 changes: 56 additions & 0 deletions internal/pkg/ctl/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright © 2018 Bitgrip <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ctl

import "testing"

func Test_isSupportedAPIVersion(t *testing.T) {
type args struct {
apiVersion string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "smaller_minor",
args: args{apiVersion: "1.0"},
want: true,
},
{
name: "equal_minor",
args: args{apiVersion: "1.2"},
want: true,
},
{
name: "bigger_minor",
args: args{apiVersion: "1.3"},
want: false,
},
}
oldVersion := Version
Version = "v1.2.0-local"
defer func() {
Version = oldVersion
}()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isSupportedAPIVersion(tt.args.apiVersion); got != tt.want {
t.Errorf("isSupportedAPIVersion() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions sample/job/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
api_version: v1.1
kind: Job
metadata:
project_name: {{ .project_name }}
namespace: {{ .project_name }}-first-namespace
project_name: "{{ .project_name }}"
namespace: "{{ .project_name }}-first-namespace"
spec:
name: pi
containers:
Expand Down
2 changes: 1 addition & 1 deletion sample/project-with-app-with-values-yaml/project.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
api_version: v1.0
api_version: v1.2
kind: Project
metadata:
name: "{{ .project_name }}"
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/Masterminds/semver/.travis.yml

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

Loading

0 comments on commit 7e3cbab

Please sign in to comment.