Skip to content

Commit

Permalink
Merge pull request kubevirt#1025 from SchSeba/master
Browse files Browse the repository at this point in the history
Add version command to virtctl
  • Loading branch information
rmohr authored May 23, 2018
2 parents bf5188b + 1926967 commit 6fa24ad
Show file tree
Hide file tree
Showing 19 changed files with 610 additions and 9 deletions.
13 changes: 13 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3005,6 +3005,19 @@
}
}
}
},
"/apis/subresources.kubevirt.io/v1alpha1/version": {
"get": {
"produces": [
"application/json"
],
"operationId": "version",
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"definitions": {
Expand Down
10 changes: 9 additions & 1 deletion cmd/subresource-access-test/subresource-access-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ package main
import (
"flag"
"fmt"
"os"

"k8s.io/client-go/rest"

"kubevirt.io/kubevirt/pkg/kubecli"
)
Expand All @@ -37,8 +40,13 @@ func main() {
}

restClient := client.RestClient()
var result rest.Result

result := restClient.Get().Resource("virtualmachines").Namespace("default").Name("fake").SubResource("test").Do()
if os.Args[1] == "version" {
result = restClient.Get().Resource("version").Do()
} else {
result = restClient.Get().Resource("virtualmachines").Namespace("default").Name("fake").SubResource("test").Do()
}

err = result.Error()
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions hack/build-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set -e

source hack/common.sh
source hack/config.sh
source hack/version.sh

if [ -z "$1" ]; then
target="install"
Expand Down Expand Up @@ -79,13 +80,13 @@ for arg in $args; do

# always build and link the linux/amd64 binary
LINUX_NAME=${ARCH_BASENAME}-linux-amd64
GOOS=linux GOARCH=amd64 go build -o ${CMD_OUT_DIR}/${BIN_NAME}/${LINUX_NAME}
GOOS=linux GOARCH=amd64 go build -o ${CMD_OUT_DIR}/${BIN_NAME}/${LINUX_NAME} -ldflags "$(kubevirt::version::ldflags)"
(cd ${CMD_OUT_DIR}/${BIN_NAME} && ln -sf ${LINUX_NAME} ${BIN_NAME})

# build virtctl also for darwin and windows
if [ "${BIN_NAME}" = "virtctl" ]; then
GOOS=darwin GOARCH=amd64 go build -o ${CMD_OUT_DIR}/${BIN_NAME}/${ARCH_BASENAME}-darwin-amd64
GOOS=windows GOARCH=amd64 go build -o ${CMD_OUT_DIR}/${BIN_NAME}/${ARCH_BASENAME}-windows-amd64.exe
GOOS=darwin GOARCH=amd64 go build -o ${CMD_OUT_DIR}/${BIN_NAME}/${ARCH_BASENAME}-darwin-amd64 -ldflags "$(kubevirt::version::ldflags)"
GOOS=windows GOARCH=amd64 go build -o ${CMD_OUT_DIR}/${BIN_NAME}/${ARCH_BASENAME}-windows-amd64.exe -ldflags "$(kubevirt::version::ldflags)"
fi
)
else
Expand Down
119 changes: 119 additions & 0 deletions hack/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash
#
# Copyright 2014 The Kubernetes Authors.
#
# 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.
#
#
# Used https://github.com/kubernetes/kubernetes/blob/master/hack/lib/version.sh as a template

# -----------------------------------------------------------------------------
# Version management helpers. These functions help to set, save and load the
# following variables:
#
# KUBEVIRT_GIT_COMMIT - The git commit id corresponding to this
# source code.
# KUBEVIRT_GIT_TREE_STATE - "clean" indicates no changes since the git commit id
# "dirty" indicates source code changes after the git commit id
# "archive" indicates the tree was produced by 'git archive'
# KUBEVIRT_GIT_VERSION - "vX.Y" used to indicate the last release version.

# Grovels through git to set a set of env variables.

function kubevirt::version::get_version_vars() {
# If the kubernetes source was exported through git archive, then
# we likely don't have a git tree, but these magic values may be filled in.
if [[ '$Format:%%$' == "%" ]]; then
KUBE_GIT_COMMIT='$Format:%H$'
KUBE_GIT_TREE_STATE="archive"
# When a 'git archive' is exported, the '$Format:%D$' below will look
# something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
# can be extracted from it.
if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
KUBE_GIT_VERSION="${BASH_REMATCH[1]}"
fi
fi

local git=(git --work-tree "${KUBEVIRT_DIR}")

if [[ -n ${KUBEVIRT_GIT_COMMIT-} ]] || KUBEVIRT_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
if [[ -z ${KUBEVIRT_GIT_TREE_STATE-} ]]; then
# Check if the tree is dirty. default to dirty
if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
KUBEVIRT_GIT_TREE_STATE="clean"
else
KUBEVIRT_GIT_TREE_STATE="dirty"
fi
fi

# Use git describe to find the version based on tags.
if [[ -n ${KUBEVIRT_GIT_VERSION-} ]] || KUBEVIRT_GIT_VERSION=$("${git[@]}" describe --tags --abbrev=14 "${KUBEVIRT_GIT_COMMIT}^{commit}" 2>/dev/null); then
# This translates the "git describe" to an actual semver.org
# compatible semantic version that looks something like this:
# v1.1.0-alpha.0.6+84c76d1142ea4d
#
# TODO: We continue calling this "git version" because so many
# downstream consumers are expecting it there.
DASHES_IN_VERSION=$(echo "${KUBEVIRT_GIT_VERSION}" | sed "s/[^-]//g")
if [[ "${DASHES_IN_VERSION}" == "---" ]]; then
# We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
KUBEVIRT_GIT_VERSION=$(echo "${KUBEVIRT_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
elif [[ "${DASHES_IN_VERSION}" == "--" ]]; then
# We have distance to base tag (v1.1.0-1-gCommitHash)
KUBEVIRT_GIT_VERSION=$(echo "${KUBEVIRT_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
fi
if [[ "${KUBEVIRT_GIT_TREE_STATE}" == "dirty" ]]; then
# git describe --dirty only considers changes to existing files, but
# that is problematic since new untracked .go files affect the build,
# so use our idea of "dirty" from git status instead.
KUBEVIRT_GIT_VERSION+="-dirty"
fi

# If KUBEVIRT_GIT_VERSION is not a valid Semantic Version, then refuse to build.
if ! [[ "${KUBEVIRT_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
echo "KUBEVIRT_GIT_VERSION should be a valid Semantic Version"
echo "Please see more details here: https://semver.org"
exit 1
fi
fi
fi
}

function kubevirt::version::ldflag() {
local key=${1}
local val=${2}

echo "-X kubevirt.io/kubevirt/pkg/version.${key}=${val}"
}

# Prints the value that needs to be passed to the -ldflags parameter of go build
function kubevirt::version::ldflags() {
kubevirt::version::get_version_vars

SOURCE_DATE_EPOCH=$(git show -s --format=format:%ct HEAD)

local buildDate
[[ -z ${SOURCE_DATE_EPOCH-} ]] || buildDate="--date=@${SOURCE_DATE_EPOCH}"
local -a ldflags=($(kubevirt::version::ldflag "buildDate" "$(date ${buildDate} -u +'%Y-%m-%dT%H:%M:%SZ')"))
if [[ -n ${KUBEVIRT_GIT_COMMIT-} ]]; then
ldflags+=($(kubevirt::version::ldflag "gitCommit" "${KUBEVIRT_GIT_COMMIT}"))
ldflags+=($(kubevirt::version::ldflag "gitTreeState" "${KUBEVIRT_GIT_TREE_STATE}"))
fi

if [[ -n ${KUBEVIRT_GIT_VERSION-} ]]; then
ldflags+=($(kubevirt::version::ldflag "gitVersion" "${KUBEVIRT_GIT_VERSION}"))
fi

# The -ldflags parameter takes a single string, so join the output.
echo "${ldflags[*]-}"
}
38 changes: 38 additions & 0 deletions manifests/dev/rbac.authorization.k8s.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,41 @@ rules:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubevirt.io: ""
kubernetes.io/bootstrapping: rbac-defaults
name: kubevirt.io:default
rules:
- apiGroups:
- subresources.kubevirt.io
resources:
- version
verbs:
- get
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
kubevirt.io: ""
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
name: kubevirt.io:default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubevirt.io:default
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:authenticated
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:unauthenticated
38 changes: 38 additions & 0 deletions manifests/release/kubevirt.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,44 @@ subjects:
name: kubevirt-privileged
namespace: {{.Namespace}}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubevirt.io: ""
kubernetes.io/bootstrapping: rbac-defaults
name: kubevirt.io:default
rules:
- apiGroups:
- subresources.kubevirt.io
resources:
- version
verbs:
- get
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
kubevirt.io: ""
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
name: kubevirt.io:default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubevirt.io:default
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:authenticated
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:unauthenticated
---
apiVersion: v1
kind: Service
metadata:
Expand Down
10 changes: 10 additions & 0 deletions pkg/kubecli/generated_mock_kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func (_mr *_MockKubevirtClientRecorder) OfflineVirtualMachine(arg0 interface{})
return _mr.mock.ctrl.RecordCall(_mr.mock, "OfflineVirtualMachine", arg0)
}

func (_m *MockKubevirtClient) ServerVersion() *ServerVersion {
ret := _m.ctrl.Call(_m, "ServerVersion")
ret0, _ := ret[0].(*ServerVersion)
return ret0
}

func (_mr *_MockKubevirtClientRecorder) ServerVersion() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "ServerVersion")
}

func (_m *MockKubevirtClient) RestClient() *rest.RESTClient {
ret := _m.ctrl.Call(_m, "RestClient")
ret0, _ := ret[0].(*rest.RESTClient)
Expand Down
1 change: 1 addition & 0 deletions pkg/kubecli/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type KubevirtClient interface {
VM(namespace string) VMInterface
ReplicaSet(namespace string) ReplicaSetInterface
OfflineVirtualMachine(namespace string) OfflineVirtualMachineInterface
ServerVersion() *ServerVersion
RestClient() *rest.RESTClient
kubernetes.Interface
}
Expand Down
63 changes: 63 additions & 0 deletions pkg/kubecli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2018 Red Hat, Inc.
*
*/

package kubecli

import (
"encoding/json"
"net/url"

"k8s.io/client-go/rest"

"kubevirt.io/kubevirt/pkg/version"
)

func (k *kubevirt) ServerVersion() *ServerVersion {
return &ServerVersion{
restClient: k.restClient,
resource: "version",
}
}

type ServerVersion struct {
restClient *rest.RESTClient
resource string
}

func (v *ServerVersion) Get() (*version.Info, error) {
result := v.restClient.Get().RequestURI("/apis/subresources.kubevirt.io/v1alpha1/version").Do()
data, err := result.Raw()
if err != nil {
connErr, isConnectionErr := err.(*url.Error)

if isConnectionErr {
return nil, connErr.Err
}

return nil, err
}

var serverInfo version.Info
json.Unmarshal(data, &serverInfo)
if err != nil {
return nil, err
}

return &serverInfo, nil
}
Loading

0 comments on commit 6fa24ad

Please sign in to comment.