Skip to content

Commit

Permalink
CR-21047-helm-migrate (#717)
Browse files Browse the repository at this point in the history
## What
added "migrate" command

## Why
migrate cli-runtime resources to helm-runtime structure (git-sources and
remote argo-rollouts installations)

## Notes
also updated go.mod and dockerfile to use golang 1.21.3

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
ATGardner and dependabot[bot] authored Nov 12, 2023
1 parent c4160f4 commit 752b573
Show file tree
Hide file tree
Showing 30 changed files with 1,349 additions and 266 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* [email protected] [email protected] [email protected] pavel@codefresh.io [email protected] itai@codefresh.io [email protected]
* [email protected] [email protected] [email protected] [email protected]
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-alpine3.16 as base
FROM golang:1.21.4-alpine3.18 as base

WORKDIR /go/src/github.com/codefresh-io/cli-v2

Expand Down Expand Up @@ -26,7 +26,7 @@ RUN go mod verify

############################### CLI ###############################
### Compile
FROM golang:1.19-alpine3.16 as codefresh-build
FROM golang:1.21.4-alpine3.18 as codefresh-build

WORKDIR /go/src/github.com/codefresh-io/cli-v2

Expand All @@ -44,7 +44,7 @@ ARG SEGMENT_WRITE_KEY
RUN make local DEV_MODE=false SEGMENT_WRITE_KEY=${SEGMENT_WRITE_KEY}

### Run
FROM alpine:3.16 as codefresh
FROM alpine:3.18 as codefresh

WORKDIR /go/src/github.com/codefresh-io/cli-v2

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.1.51
VERSION=v0.1.52

OUT_DIR=dist
YEAR?=$(shell date +"%Y")
Expand Down Expand Up @@ -176,4 +176,4 @@ $(GOBIN)/mockgen:
$(GOBIN)/golangci-lint:
@mkdir dist || true
@echo installing: golangci-lint
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.52.2
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.55.2
6 changes: 3 additions & 3 deletions build/Dockerfile.helper
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21.1-alpine3.18
FROM golang:1.21.4-alpine3.18

RUN apk -U add --no-cache \
bash \
Expand All @@ -12,14 +12,14 @@ RUN apk -U add --no-cache \
openssl \
&& update-ca-certificates

ARG GH_VERSION=2.35.0
ARG GH_VERSION=2.38.0
RUN curl -L https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz --output gh.tar.gz \
&& tar -xzf gh.tar.gz \
&& mv gh_${GH_VERSION}_linux_amd64/bin/gh /usr/local/bin \
&& rm gh.tar.gz \
&& rm -rf gh_${GH_VERSION}_linux_amd64

ARG KUSTOMIZE_VERSION=5.1.1
ARG KUSTOMIZE_VERSION=5.2.1
RUN curl -Ls https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv${KUSTOMIZE_VERSION}/kustomize_v${KUSTOMIZE_VERSION}_linux_amd64.tar.gz --output kustomize.tar.gz \
&& tar -xzf kustomize.tar.gz \
&& mv ./kustomize /usr/bin \
Expand Down
2 changes: 1 addition & 1 deletion build/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
prepare_env_vars: &deps
stage: Prepare
title: prepare-env
image: quay.io/codefresh/golang-ci-helper:1.20.3
image: quay.io/codefresh/golang-ci-helper:1.21.4
commands:
- cf_export GO111MODULE=on
- cf_export GOCACHE=/codefresh/volume/gocache # change gopath to codefresh shared volume
Expand Down
10 changes: 5 additions & 5 deletions cmd/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func newClusterAddCommand() *cobra.Command {

ctx := cmd.Context()

opts.runtimeName, err = ensureRuntimeName(ctx, args, true)
opts.runtimeName, err = ensureRuntimeName(ctx, args, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -451,7 +451,7 @@ func newClusterRemoveCommand() *cobra.Command {

ctx := cmd.Context()

opts.runtimeName, err = ensureRuntimeName(ctx, args, true)
opts.runtimeName, err = ensureRuntimeName(ctx, args, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -523,12 +523,12 @@ func runClusterList(ctx context.Context, runtimeName string) error {

sort.SliceStable(clusters, func(i, j int) bool {
c1 := clusters[i]
if c1.Metadata.Name == "in-cluster" {
if c1.Metadata.Name == store.Get().InClusterName {
return true
}

c2 := clusters[j]
if c2.Metadata.Name == "in-cluster" {
if c2.Metadata.Name == store.Get().InClusterName {
return false
}

Expand Down Expand Up @@ -597,7 +597,7 @@ func newClusterCreateArgoRolloutsCommand() *cobra.Command {
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error

opts.runtimeName, err = ensureRuntimeName(cmd.Context(), args, true)
opts.runtimeName, err = ensureRuntimeName(cmd.Context(), args, nil)
return err
},
RunE: func(cmd *cobra.Command, _ []string) error {
Expand Down
35 changes: 17 additions & 18 deletions cmd/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func getRepoFromUserInput(cmd *cobra.Command) error {
return cmd.Flags().Set("repo", repoInput)
}

func ensureRuntimeName(ctx context.Context, args []string, allowManaged bool) (string, error) {
func ensureRuntimeName(ctx context.Context, args []string, filter func(runtime *platmodel.Runtime) bool) (string, error) {
var (
runtimeName string
err error
Expand All @@ -190,7 +190,7 @@ func ensureRuntimeName(ctx context.Context, args []string, allowManaged bool) (s
}

if !store.Get().Silent {
runtimeName, err = getRuntimeNameFromUserSelect(ctx, allowManaged)
runtimeName, err = getRuntimeNameFromUserSelect(ctx, filter)
if err != nil {
return "", err
}
Expand All @@ -203,7 +203,7 @@ func ensureRuntimeName(ctx context.Context, args []string, allowManaged bool) (s
return runtimeName, nil
}

func getRuntimeNameFromUserSelect(ctx context.Context, allowManaged bool) (string, error) {
func getRuntimeNameFromUserSelect(ctx context.Context, filter func(runtime *platmodel.Runtime) bool) (string, error) {
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
if err != nil {
return "", err
Expand All @@ -213,35 +213,34 @@ func getRuntimeNameFromUserSelect(ctx context.Context, allowManaged bool) (strin
return "", fmt.Errorf("no runtimes were found")
}

var runtimeNames []string

for _, rt := range runtimes {
rtDisplay := rt.Metadata.Name
if rt.Managed {
if !allowManaged {
// preventing hosted runtimes to prompt
continue
var filteredRuntimes []platmodel.Runtime
if filter != nil {
filteredRuntimes = make([]platmodel.Runtime, 0)
for _, rt := range runtimes {
if filter(&rt) {
filteredRuntimes = append(filteredRuntimes, rt)
}
rtDisplay = fmt.Sprintf("%s (hosted)", rtDisplay)
}
runtimeNames = append(runtimeNames, rtDisplay)
} else {
filteredRuntimes = runtimes
}

templates := &promptui.SelectTemplates{
Selected: "{{ . | yellow }} ",
Active: fmt.Sprintf("%s {{ .Metadata.Name | underline }}{{ if ne .InstallationType \"HELM\" }}{{ printf \" (%%s)\" .InstallationType | underline }}{{ end }}", promptui.IconSelect),
Inactive: " {{ .Metadata.Name }}{{ if ne .InstallationType \"HELM\" }}{{ printf \" (%s)\" .InstallationType }}{{ end }}",
Selected: "{{ .Metadata.Name | yellow }}",
}

labelStr := fmt.Sprintf("%vSelect runtime%v", CYAN, COLOR_RESET)

prompt := promptui.Select{
Label: labelStr,
Items: runtimeNames,
Items: filteredRuntimes,
Templates: templates,
}

_, result, err := prompt.Run()
resultSplit := strings.Split(result, " ")
return resultSplit[0], err
i, _, err := prompt.Run()
return filteredRuntimes[i].Metadata.Name, err
}

func getRuntimeNameFromUserInput() (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/git-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
err = appProxy.AppProxyGitSources().Create(ctx, &apmodel.CreateGitSourceInput{
AppName: opts.GsName,
AppSpecifier: appSpecifier,
DestServer: store.Get().InCluster,
DestServer: store.Get().InClusterServerURL,
DestNamespace: &opts.RuntimeNamespace,
IsInternal: &isInternal,
Include: &opts.Include,
Expand Down
15 changes: 8 additions & 7 deletions cmd/commands/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

cfgit "github.com/codefresh-io/cli-v2/pkg/git"
"github.com/codefresh-io/cli-v2/pkg/log"
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"
"github.com/codefresh-io/cli-v2/pkg/util/helm"
"github.com/codefresh-io/cli-v2/pkg/util/kube"
Expand All @@ -49,10 +50,6 @@ type (
}
)

const (
CODEFRESH_TOKEN = "codefresh-token"
)

var (
ErrRuntimeTokenNotFound = errors.New("runtime token not found")
)
Expand Down Expand Up @@ -97,7 +94,7 @@ func NewHelmValidateValuesCommand() *cobra.Command {

cmd.Flags().StringVarP(&opts.valuesFile, "values", "f", "", "specify values in a YAML file or a URL")
cmd.Flags().BoolVar(&opts.hook, "hook", false, "set to true when running inside a helm-hook")
opts.helm = helm.AddFlags(cmd.Flags())
opts.helm, _ = helm.AddFlags(cmd.Flags())
opts.kubeFactory = apkube.AddFlags(cmd.Flags())

util.Die(cmd.Flags().MarkHidden("hook"))
Expand Down Expand Up @@ -165,7 +162,7 @@ func checkPlatform(ctx context.Context, opts *HelmValidateValuesOptions, values
}

func validateWithRuntimeToken(ctx context.Context, opts *HelmValidateValuesOptions, codefreshValues chartutil.Values, runtimeName string) error {
runtimeToken, _ := kube.GetValueFromSecret(ctx, opts.kubeFactory, opts.namespace, CODEFRESH_TOKEN, "token")
runtimeToken, _ := kube.GetValueFromSecret(ctx, opts.kubeFactory, opts.namespace, store.Get().CFTokenSecret, "token")
if runtimeToken == "" {
return ErrRuntimeTokenNotFound
}
Expand Down Expand Up @@ -201,7 +198,7 @@ func validateWithUserToken(ctx context.Context, opts *HelmValidateValuesOptions,
return "", "", err
}

if !user.IsActiveAccountAdmin() {
if !user.IsActiveAccountAdmin() {
return "", "", fmt.Errorf("user \"%s\" does not have Admin role in account \"%s\"", user.Name, *user.ActiveAccount.Name)
}

Expand Down Expand Up @@ -565,3 +562,7 @@ func getValueFromSecretKeyRef(ctx context.Context, opts *HelmValidateValuesOptio

return kube.GetValueFromSecret(ctx, opts.kubeFactory, opts.namespace, name, key)
}

func filterOnlyClidRuntime(rt *platmodel.Runtime) bool {
return rt.InstallationType == platmodel.InstallationTypeCli
}
Loading

0 comments on commit 752b573

Please sign in to comment.