Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow inline environments to be pruned #511

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/tk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func pruneCmd() *cli.Command {
var opts tanka.PruneOpts
cmd.Flags().BoolVar(&opts.Force, "force", false, "force deleting (kubectl delete --force)")
cmd.Flags().BoolVar(&opts.AutoApprove, "dangerous-auto-approve", false, "skip interactive approval. Only for automation!")
cmd.Flags().StringVar(&opts.Name, "name", "", "Selects an environment from inline environments")
getJsonnetOpts := jsonnetFlags(cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
Expand Down
10 changes: 8 additions & 2 deletions pkg/spec/v1alpha1/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package v1alpha1

import "strings"
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)

// New creates a new Environment object with internal values already set
func New() *Environment {
Expand Down Expand Up @@ -46,7 +50,9 @@ func (m Metadata) Get(label string) (value string) {
}

func (m Metadata) NameLabel() string {
return strings.Replace(m.Name, "/", ".", -1)
partsHash := sha256.Sum256([]byte(fmt.Sprintf("%s:%s", m.Name, m.Namespace)))
chars := []rune(hex.EncodeToString(partsHash[:]))
return string(chars[:48])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no science at all to my choice here of 48 🤷‍♂️

}

// Spec defines Kubernetes properties
Expand Down
5 changes: 5 additions & 0 deletions pkg/tanka/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ func TestLoad(t *testing.T) {
})
}
}

func TestLoadFailsWhenDuplicateEnv(t *testing.T) {
_, err := Load("./testdata/cases/withduplicateenv", Opts{Name: "withenv"})
assert.NotNil(t, err)
}
34 changes: 34 additions & 0 deletions pkg/tanka/testdata/cases/withduplicateenv/main.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
env1: {
apiVersion: 'tanka.dev/v1alpha1',
kind: 'Environment',
metadata: {
name: 'withenv',
},
spec: {
apiServer: 'https://localhost',
namespace: 'withenv',
},
data: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: { name: 'config' },
},
},
env2: {
apiVersion: 'tanka.dev/v1alpha1',
kind: 'Environment',
metadata: {
name: 'withenv',
},
spec: {
apiServer: 'https://localhost',
namespace: 'withenv',
},
data: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: { name: 'config' },
},
},
}