Skip to content

Commit

Permalink
Update k8s-kms-plugin version command
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas-Peiffer <[email protected]>
  • Loading branch information
Nicolas-Peiffer committed Dec 16, 2024
1 parent ca4b69c commit 2a9cff0
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 114 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ all: build

# Project name
PROJECT_NAME := k8s-kms-plugin
REPOSITORY_NAME := "github.com/ThalesGroup/$(PROJECT_NAME)"
GO_MODULE_NAME := "github.com/ThalesGroup/$(PROJECT_NAME)"

# Useful variables for build metadata
VERSION ?= $(shell git describe --tags --always)
COMMIT_LONG ?= $(shell git rev-parse HEAD)
COMMIT_SHORT ?= $(shell git rev-parse --short=8 HEAD)
COMMIT_TIMESTAMP := $(shell git show -s --format=%cI HEAD)
GO_VERSION ?= $(shell go version)
BUILD_PLATFORM ?= $(shell uname -m)
BUILD_PLATFORM ?= $(shell uname -m)
BUILD_DATE ?= $(shell date -Iseconds)
LDFLAGS = "-X '$(REPOSITORY_NAME)/cmd/k8s-kms-plugin/cmd.RawGitVersion=$(VERSION)' -X '$(REPOSITORY_NAME)/cmd/k8s-kms-plugin/cmd.CommitVersionIdLong=$(COMMIT_LONG)' -X '$(REPOSITORY_NAME)/cmd/k8s-kms-plugin/cmd.CommitVersionIdShort=$(COMMIT_SHORT)' -X '$(REPOSITORY_NAME)/cmd/k8s-kms-plugin/cmd.GoVersion=$(GO_VERSION)' -X '$(REPOSITORY_NAME)/cmd/k8s-kms-plugin/cmd.BuildPlatform=$(BUILD_PLATFORM)' -X '$(REPOSITORY_NAME)/cmd/k8s-kms-plugin/cmd.BuildDate=$(BUILD_DATE)'"
LDFLAGS = "-X '$(GO_MODULE_NAME)/pkg/version.RawGitDescribe=$(VERSION)' -X '$(GO_MODULE_NAME)/pkg/version.GitCommitIdLong=$(COMMIT_LONG)' -X '$(GO_MODULE_NAME)/pkg/version.GitCommitIdShort=$(COMMIT_SHORT)' -X '$(GO_MODULE_NAME)/pkg/version.GoVersion=$(GO_VERSION)' -X '$(GO_MODULE_NAME)/pkg/version.BuildPlatform=$(BUILD_PLATFORM)' -X '$(GO_MODULE_NAME)/pkg/version.BuildDate=$(BUILD_DATE)' -X '$(GO_MODULE_NAME)/pkg/version.GitCommitTimestamp=$(COMMIT_TIMESTAMP)'"
GO_LDFLAGS = -ldflags=$(LDFLAGS)
BINARY_NAME = $(PROJECT_NAME)

# For dev
SECRET_NAME=gcr-json-key
P11_TOKEN=ajak
Expand Down
160 changes: 54 additions & 106 deletions cmd/k8s-kms-plugin/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,129 +1,77 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
// MIT License
//
// Copyright (c) 2024 Thales. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"encoding/json"
"fmt"
"log/slog"

"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
"github.com/ThalesGroup/k8s-kms-plugin/pkg/version"

var (
RawGitVersion string
CommitVersionIdShort string
CommitVersionIdLong string
OutputFormat string
GoVersion string
BuildPlatform string
BuildDate string
"github.com/spf13/cobra"
)

type JsonVersion struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Version string `json:"version"`
CommitIdLong string `json:"commitIdLong"`
CommitIdShort string `json:"commitIdShort"`
GoVersion string `json:"goVersion"`
Date string `json:"date"`
Platorm string `json:"plaform"`
}
type YamlVersion struct {
Major int64 `yaml:"major"`
Minor int64 `yaml:"minor"`
Version string `yaml:"version"`
CommitIdLong string `yaml:"commitIdLong"`
CommitIdShort string `yaml:"commitIdShort"`
GoVersion string `yaml:"goVersion"`
Date string `yaml:"date"`
Platorm string `yaml:"plaform"`
}

func validateInputs() {
if OutputFormat != "" && OutputFormat != "json" && OutputFormat != "yaml" {
OutputFormat = ""
}
}
func CreateJsonVersion() []byte {
version := semver.New(RawGitVersion)

jsonFormat := &JsonVersion{
Major: version.Major,
Minor: version.Minor,
Version: RawGitVersion,
CommitIdLong: CommitVersionIdLong,
CommitIdShort: CommitVersionIdShort,
GoVersion: GoVersion,
Date: BuildDate,
Platorm: BuildPlatform,
}
data, err := json.MarshalIndent(&jsonFormat, "", " ")
if err != nil {
// CLI options pflags names
var outputVersion string // One of 'yaml' or 'json'.

fmt.Println(err)
}
return data
}
func CreateYamlVersion() []byte {
version := semver.New(RawGitVersion)

yamlFormat := &YamlVersion{
Major: version.Major,
Minor: version.Minor,
Version: RawGitVersion,
CommitIdLong: CommitVersionIdLong,
CommitIdShort: CommitVersionIdShort,
GoVersion: GoVersion,
Date: BuildDate,
Platorm: BuildPlatform,
}
data, err := yaml.Marshal(&yamlFormat)
if err != nil {

fmt.Println(err)
}
return data
}

func generateOutput() {
if OutputFormat == "json" {
fmt.Println(string(CreateJsonVersion()))
} else if OutputFormat == "yaml" {
fmt.Println(string(CreateYamlVersion()))
} else {
fmt.Println(RawGitVersion)
}
}
// prettyPrintVersion defined by the user with flag --pretty
var prettyPrintVersion bool

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show the version of the application with the short commit sha associated",
Short: "Print the version information.",
Long: `Print the version information with various level of details
including information of the build and git repository metadata.
Examples:
# print the version information with git repository details as a one liner
# JSON string.
k8s-kms-plugin version -o json --pretty=false`,
Run: func(cmd *cobra.Command, args []string) {
if OutputFormat == "" {
fmt.Println(RawGitVersion)
} else {
validateInputs()
generateOutput()
slog.Debug("running command: k8s-kms-plugin version")

// Add logic here to check if flag output is used with JSON or YAML.
flagOutputUsed := cmd.Flags().Changed("output")
flagPrettyUsed := cmd.Flags().Changed("pretty")

if flagOutputUsed && !(outputVersion == "yaml" || outputVersion == "json") {
slog.Error("Invalid output format. Must be json or yaml.",
"output", outputVersion)
}

if flagOutputUsed && flagPrettyUsed && outputVersion == "yaml" {
slog.Error("Flag --pretty must NOT be used when output format is yaml." +
" --pretty is only used when output format is json")
} else {
fmt.Fprintln(cmd.OutOrStdout(), version.VersionOutputToString(outputVersion, prettyPrintVersion))
}
},
}

func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().StringVarP(&OutputFormat, "output", "o", "json", "'json' or 'yaml'")
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
versionCmd.Flags().StringVarP(&outputVersion, "output", "o", "", "Format of the version output. One of 'yaml' or 'json'.")
versionCmd.PersistentFlags().BoolVarP(&prettyPrintVersion, "pretty", "", true, "Activate pretty print output for JSON.")
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ require (
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools/cmd/cover v0.1.0-deprecated
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/ThalesGroup/crypto11 v1.2.6
github.com/ThalesGroup/gose v0.9.0
github.com/coreos/go-semver v0.3.1
github.com/blang/semver/v4 v4.0.0
github.com/hashicorp/go-version v1.7.0
k8s.io/kms v0.31.3
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ github.com/ThalesGroup/gose v0.9.0 h1:BVG/q1U08ZttmZ7T1aQF9eWE0XQ4xHZlTSQhgF5ov9
github.com/ThalesGroup/gose v0.9.0/go.mod h1:+GCo0u8EyLOSJhQ2G2MSw2UdfGhMWRx394EsMtAE9cA=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -149,6 +149,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4G
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0/go.mod h1:r1hZAcvfFXuYmcKyCJI9wlyOPIZUJl6FCB8Cpca/NLE=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
Loading

0 comments on commit 2a9cff0

Please sign in to comment.