Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

EVEREST-488 Added version command #174

Merged
merged 17 commits into from
Oct 16, 2023
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
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
FILES = $(shell find . -type f -name '*.go')

RELEASE_VERSION ?= $(shell git describe --always --dirty | cut -b2-)
RELEASE_FULLCOMMIT ?= $(shell git rev-parse HEAD)

LD_FLAGS = -ldflags " \
-X 'github.com/percona/percona-everest-cli/pkg/version.ProjectName=everestctl' \
-X 'github.com/percona/percona-everest-cli/pkg/version.Version=$(RELEASE_VERSION)' \
-X 'github.com/percona/percona-everest-cli/pkg/version.FullCommit=$(RELEASE_FULLCOMMIT)' \
"

default: help

help: ## Display this help message
Expand All @@ -11,7 +20,7 @@ init: ## Install development tools
cd tools && go generate -x -tags=tools

build: ## Build binaries
go build -race -o bin/everest ./cmd/everest
go build -race $(LD_FLAGS) -o bin/everest ./cmd/everest

gen: ## Generate code
go generate ./...
Expand Down Expand Up @@ -42,8 +51,8 @@ k8s: ## Create a local minikube cluster
kubectl apply -f ./dev/kubevirt-hostpath-provisioner.yaml

release:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o ./dist/everestctl-linux-amd64 ./cmd/everest
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -v -o ./dist/everestctl-linux-arm64 ./cmd/everest
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -o ./dist/everestctl-darwin-amd64 ./cmd/everest
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -v -o ./dist/everestctl-darwin-arm64 ./cmd/everest
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -v -o ./dist/everestctl.exe ./cmd/everest
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v $(LD_FLAGS) -o ./dist/everestctl-linux-amd64 ./cmd/everest
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -v $(LD_FLAGS) -o ./dist/everestctl-linux-arm64 ./cmd/everest
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v $(LD_FLAGS) -o ./dist/everestctl-darwin-amd64 ./cmd/everest
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -v $(LD_FLAGS) -o ./dist/everestctl-darwin-arm64 ./cmd/everest
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -v $(LD_FLAGS) -o ./dist/everestctl.exe ./cmd/everest
3 changes: 0 additions & 3 deletions cli-tests/tests/flow/pg-operator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ test.describe('Everest CLI install operators', async () => {
await restartedOperator.assertSuccess();

expect(operator.getStdOutLines()[0]).not.toEqual(restartedOperator.getStdOutLines()[0]);
const out = await cli.everestExecSkipWizard(
`install operators --operator.mongodb=false --operator.postgresql=true --operator.xtradb-cluster=true --backup.enable=0 --monitoring.enable=0 --name=${clusterName}`,
);
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved

await out.assertSuccess();
await out.outErrContainsNormalizedMany([
Expand Down
30 changes: 30 additions & 0 deletions cli-tests/tests/version.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// percona-everest-cli
// Copyright (C) 2023 Percona LLC
//
// 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.
import { test } from '@fixtures';

test.describe('Everest CLI "version" validation', async () => {
test('version validation', async ({ cli }) => {
const out = await cli.everestExecSilent('version');
const version = await cli.exec('git describe --always --dirty|cut -b2-');
await version.assertSuccess();

await out.assertSuccess();
await out.outContainsNormalizedMany([
'ProjectName: everestctl',
'Version: ' + version.getStdOutLines()[0],
]);
});

});
1 change: 1 addition & 0 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewRootCmd(l *zap.SugaredLogger) *cobra.Command {
// rootCmd.AddCommand(newProvisionCmd(l))
// rootCmd.AddCommand(newListCmd(l))
rootCmd.AddCommand(newDeleteCmd(l))
rootCmd.AddCommand(newVersionCmd(l))

return rootCmd
}
33 changes: 33 additions & 0 deletions commands/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"
"go.uber.org/zap"
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved

"github.com/percona/percona-everest-cli/pkg/version"
)

func newVersionCmd(l *zap.SugaredLogger) *cobra.Command {
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved
return &cobra.Command{
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
outputJSON, err := cmd.Flags().GetBool("json")
if err != nil {
l.Errorf("could not parse json global flag. Error: %s", err)
return
}
if !outputJSON {
fmt.Println(version.FullVersionInfo()) //nolint:forbidigo
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved
return
}
version, err := version.FullVersionJSON()
if err != nil {
l.Errorf("could not print JSON. Error: %s", err)
return
}
fmt.Println(version) //nolint:forbidigo
},
}
}
1 change: 0 additions & 1 deletion data/crds/olm/percona-dbaas-catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ spec:
sourceType: grpc
grpcPodConfig:
securityContextConfig: restricted
image: docker.io/percona/everest-catalog:latest
updateStrategy:
registryPoll:
interval: 45m
13 changes: 13 additions & 0 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (

"github.com/percona/percona-everest-cli/data"
"github.com/percona/percona-everest-cli/pkg/kubernetes/client"
everestVersion "github.com/percona/percona-everest-cli/pkg/version"
)

// ClusterType defines type of cluster.
Expand Down Expand Up @@ -479,6 +480,18 @@ func (k *Kubernetes) InstallPerconaCatalog(ctx context.Context) error {
if err != nil {
return errors.Join(err, errors.New("failed to read percona catalog file"))
}
o := make(map[string]interface{})
if err := yamlv3.Unmarshal(data, &o); err != nil {
return err
}

if err := unstructured.SetNestedField(o, everestVersion.CatalogImage(), "spec", "image"); err != nil {
return err
}
data, err = yamlv3.Marshal(o)
if err != nil {
return err
}

if err := k.client.ApplyFile(data); err != nil {
return errors.Join(err, errors.New("cannot apply percona catalog file"))
Expand Down
76 changes: 76 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// percona-everest-cli
// Copyright (C) 2023 Percona LLC
//
// 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 install holds the main logic for installation commands.

// Package version implements version reporting command to the end user.
package version
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"fmt"
"strings"

goversion "github.com/hashicorp/go-version"
)

const (
devCatalogImage = "docker.io/percona/everest-catalog:latest"
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved
releaseCatalogImage = "docker.io/percona/everest-catalog:%s"
)

var (
// ProjectName is a component name, e.g. everestctl.
ProjectName string //nolint:gochecknoglobals
// Version is a component version e.g. v0.3.0-1-a93bef.
Version string //nolint:gochecknoglobals
// FullCommit is a git commit hash.
FullCommit string //nolint:gochecknoglobals
// CatalogImage is a image path for OLM catalog.
catalogImage string //nolint:gochecknoglobals
)

// CatalogImage returns a catalog image needed for the build of everestctl
//
// for dev builds it returns everest-catalog:latest
// for the release it returns everest-catalog:X.Y.Z.
func CatalogImage() string {
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved
catalogImage = devCatalogImage
_, err := goversion.NewSemver(Version)
if !strings.Contains(Version, "dirty") && Version != "" && err == nil {
catalogImage = fmt.Sprintf(releaseCatalogImage, Version)
}
return catalogImage
}

// FullVersionInfo returns full version report.
func FullVersionInfo() string {
gen1us2k marked this conversation as resolved.
Show resolved Hide resolved
out := []string{
"ProjectName: " + ProjectName,
"Version: " + Version,
"FullCommit: " + FullCommit,
}
return strings.Join(out, "\n")
}

// FullVersionJSON returns version info as JSON.
func FullVersionJSON() (string, error) {
res := map[string]string{
"projectName": ProjectName,
"version": Version,
"fullCommit": FullCommit,
}
data, err := json.Marshal(res)
return string(data), err
}
18 changes: 18 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package version

import (
"fmt"
"testing"

"gotest.tools/assert"
)

func TestCatalogImage(t *testing.T) {
t.Parallel()
Version = "v0.3.0"
assert.Equal(t, CatalogImage(), fmt.Sprintf(releaseCatalogImage, Version))
Version = "v0.3.0-1-asd-dirty"
assert.Equal(t, CatalogImage(), devCatalogImage)
Version = "c09550"
assert.Equal(t, CatalogImage(), devCatalogImage)
}
Loading