Skip to content

Commit

Permalink
Merge pull request #22 from burmanm/workflow_build
Browse files Browse the repository at this point in the history
Fix caching directories to match with newer Dockerfile, add workflow
  • Loading branch information
burmanm authored Feb 15, 2024
2 parents 41b0d34 + e85b7c3 commit 41178b9
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
run: |
echo ::set-output name=sha_short::$(git rev-parse --short=8 ${{ github.sha }})
echo ::set-output name=tag_name::${GITHUB_REF#refs/tags/}
mkdir -p build/
- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
Expand All @@ -66,5 +67,6 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: k8ssandra/k8ssandra-client:${{ steps.vars.outputs.sha_short }}
platforms: linux/amd64,linux/arm64
context: .
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
8 changes: 4 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
name: Cass Operator Release
name: k8ssandra-client Release

on:
push:
tags:
- 'v*.*.*'
workflow_dispatch:
# Allow to rerun this for a tag to get updated UBI-images

jobs:
release_cass_operator:
release_k8ssandra_client:
name: Release Docker Image
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -42,13 +40,15 @@ jobs:
echo "sha_short=$(git rev-parse --short=8 ${{ github.sha }})" >> $GITHUB_OUTPUT
echo "tag_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
echo "TARGET_VERSION=$(echo ${GITHUB_REF#refs/tags/} | awk '{print substr($0,2)}')" >> $GITHUB_ENV
mkdir -p build/
- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
load: false
file: cmd/kubectl-k8ssandra/Dockerfile
push: true
context: .
tags: k8ssandra/k8ssandra-client:${{ steps.vars.outputs.tag_name }}, ghcr.io/k8ssandra/k8ssandra-client:${{ steps.vars.outputs.tag_name }}
platforms: linux/amd64,linux/arm64
cache-from: type=local,src=/tmp/.buildx-cache
Expand Down
93 changes: 93 additions & 0 deletions .github/workflows/tools-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: k8ssandra-client build for external calls
on:
workflow_dispatch:
inputs:
chartName:
description: 'Name of the chart to be embedded in the image'
required: true
type: string
chartVersion:
description: 'Target version to upgrade CRDs to. If used with git, give the tag name'
required: true
type: string
repoName:
description: 'Name of the Helm repository if not default'
required: false
type: string
repoURL:
description: 'URL of the target repository if repoName is set'
required: false
type: string
gitRepo:
description: 'git repository to be embedded in the image'
required: false
type: string
chartDir:
description: 'Directory of the chart in the git repository to be embedded in the image'
required: false
type: string
tokenSecret:
description: 'Secret name to be used as GitHub token if gitRepo is set'
required: false
type: string
jobs:
create_the_image:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets[format('{0}', inputs.tokenSecret)] }}
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- uses: azure/setup-helm@v3
if: ${{ env.ACT }}
with:
version: '3.13.1'
id: install
- name: Set up QEMU
if: ${{ !env.ACT }}
uses: docker/setup-qemu-action@v2
- name: Login to DockerHub
if: ${{ !env.ACT }}
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Create release name
id: vars
run: |
echo "tag=${{ inputs.chartName }}-${{ inputs.chartVersion }}" >> $GITHUB_OUTPUT
- name: Download the requested charts
if: ${{ !inputs.gitRepo }}
id: download_charts
run: |
helm repo add ${{ inputs.repoName }} ${{ inputs.repoURL }}
helm repo update
helm pull ${{ inputs.repoName }}/${{ inputs.chartName }} --version ${{ inputs.chartVersion }}
mkdir -p build/${{ inputs.repoName }}
tar -xvf ${{ inputs.chartName }}-${{ inputs.chartVersion }}.tgz -C build/${{ inputs.repoName }}
- name: Download the requested chart from another repository
if: ${{ inputs.gitRepo }}
id: download_charts_github
run: |
gh repo clone ${{ inputs.gitRepo }} ${{ inputs.repoName }}
cd ${{ inputs.repoName }}
git checkout ${{ inputs.chartVersion }}
cd ..
mkdir -p build/${{ inputs.repoName }}
cp -R ${{ inputs.repoName }}/${{ inputs.chartDir }}/* build/${{ inputs.repoName }}
helm dependency update build/${{ inputs.repoName }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
load: false
file: cmd/kubectl-k8ssandra/Dockerfile
push: ${{ github.event_name != 'pull_request' && !env.ACT }}
tags: k8ssandra/k8ssandra-client:${{ steps.vars.outputs.tag }}
platforms: linux/amd64,linux/arm64
context: .
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ bin/
# Local History for Visual Studio Code
.history/
.vscode/
build/
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ build: test ## Build kubectl-k8ssandra

.PHONY: docker-build
docker-build: ## Build k8ssandra-client docker image
mkdir -p build/
docker buildx build --build-arg VERSION=${VERSION} -t ${IMG} . --load -f cmd/kubectl-k8ssandra/Dockerfile

.PHONY: kind-load
Expand Down
9 changes: 7 additions & 2 deletions cmd/kubectl-k8ssandra/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the binary
FROM golang:1.20 as builder
FROM golang:1.21 as builder
ARG TARGETOS
ARG TARGETARCH

Expand All @@ -14,11 +14,15 @@ RUN go mod download
COPY cmd/ cmd/
COPY pkg/ pkg/

# Copy the cached helm charts
RUN mkdir -p build/
COPY build/ build/

# Build
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o kubectl-k8ssandra cmd/kubectl-k8ssandra/main.go

# Build the UBI image
FROM redhat/ubi8-micro:latest
FROM redhat/ubi8-minimal:latest

ARG VERSION

Expand All @@ -32,6 +36,7 @@ LABEL description="Part of the toolset for the DataStax Kubernetes Operator for

WORKDIR /
COPY --from=builder /workspace/kubectl-k8ssandra .
COPY --from=builder --chown=65532:65532 /workspace/build/ .cache/
COPY ./LICENSE /licenses/

USER 65532:65532
Expand Down
44 changes: 24 additions & 20 deletions cmd/kubectl-k8ssandra/helm/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ import (

var (
upgraderExample = `
# update CRDs in the namespace to targetVersion
%[1]s crds --chartName <chartName> --targetVersion <targetVersion> [<args>]
# update CRDs in the namespace to chartVersion
%[1]s upgrade --chartName <chartName> --chartVersion <chartVersion> [<args>]
# update CRDs in the namespace to targetVersion with non-default chartRepo (helm.k8ssandra.io)
%[1]s crds --chartName <chartName> --targetVersion <targetVersion> --chartRepo <repository> [<args>]
# update CRDs in the namespace to chartVersion with non-default chartRepo (helm.k8ssandra.io)
%[1]s upgrade --chartName <chartName> --chartVersion <chartVersion> --chartRepo <repository> [<args>]
`
errNotEnoughParameters = fmt.Errorf("not enough parameters, requires chartName and targetVersion")
errNotEnoughParameters = fmt.Errorf("not enough parameters, requires chartName and chartVersion")
)

type options struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
namespace string
chartName string
targetVersion string
chartRepo string
repoURL string
namespace string
chartName string
chartVersion string
chartRepo string
repoURL string
download bool
}

func newOptions(streams genericclioptions.IOStreams) *options {
Expand All @@ -43,8 +44,8 @@ func NewUpgradeCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(streams)

cmd := &cobra.Command{
Use: "upgrade <targetVersion> [flags]",
Short: "upgrade k8ssandra CRDs to target release version",
Use: "upgrade [flags]",
Short: "upgrade CRDs from chart to target version",
Example: fmt.Sprintf(upgraderExample, "kubectl k8ssandra helm crds"),
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
Expand All @@ -64,9 +65,10 @@ func NewUpgradeCmd(streams genericclioptions.IOStreams) *cobra.Command {

fl := cmd.Flags()
fl.StringVar(&o.chartName, "chartName", "", "chartName to upgrade")
fl.StringVar(&o.targetVersion, "targetVersion", "", "targetVersion to upgrade to")
fl.StringVar(&o.chartVersion, "chartVersion", "", "chartVersion to upgrade to")
fl.StringVar(&o.chartRepo, "chartRepo", "", "optional chart repository name to override the default (k8ssandra)")
fl.StringVar(&o.repoURL, "repoURL", "", "optional chart repository url to override the default (helm.k8ssandra.io)")
fl.BoolVar(&o.download, "download", false, "only download the chart")
o.configFlags.AddFlags(fl)

return cmd
Expand All @@ -75,7 +77,7 @@ func NewUpgradeCmd(streams genericclioptions.IOStreams) *cobra.Command {
// Complete parses the arguments and necessary flags to options
func (c *options) Complete(cmd *cobra.Command, args []string) error {
var err error
if len(args) < 2 {
if c.chartName == "" && c.chartVersion == "" {
return errNotEnoughParameters
}

Expand All @@ -87,14 +89,13 @@ func (c *options) Complete(cmd *cobra.Command, args []string) error {
c.chartRepo = helmutil.K8ssandraRepoName
}

c.targetVersion = args[0]
c.namespace, _, err = c.configFlags.ToRawKubeConfigLoader().Namespace()
return err
}

// Validate ensures that all required arguments and flag values are provided
func (c *options) Validate() error {
// TODO Validate that the targetVersion is valid
// TODO Validate that the chartVersion is valid
return nil
}

Expand All @@ -105,9 +106,12 @@ func (c *options) Run() error {
return err
}

kubeClient, err := kubernetes.GetClientInNamespace(restConfig, c.namespace)
if err != nil {
return err
var kubeClient kubernetes.NamespacedClient
if !c.download {
kubeClient, err = kubernetes.GetClientInNamespace(restConfig, c.namespace)
if err != nil {
return err
}
}

ctx := context.Background()
Expand All @@ -117,6 +121,6 @@ func (c *options) Run() error {
return err
}

_, err = upgrader.Upgrade(ctx, c.targetVersion)
_, err = upgrader.Upgrade(ctx, c.chartVersion)
return err
}
2 changes: 1 addition & 1 deletion cmd/kubectl-k8ssandra/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewHelmCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := NewHelmOptions(streams)

cmd := &cobra.Command{
Use: "k8ssandra [subcommand] [flags]",
Use: "helm [subcommand] [flags]",
}

// Add subcommands
Expand Down
2 changes: 2 additions & 0 deletions cmd/kubectl-k8ssandra/k8ssandra/k8ssandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// "github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/migrate"
// "github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/nodetool"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/config"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/helm"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/operate"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/users"

Expand Down Expand Up @@ -51,6 +52,7 @@ func NewCmd(streams genericclioptions.IOStreams) *cobra.Command {
cmd.AddCommand(users.NewCmd(streams))
// cmd.AddCommand(migrate.NewInstallCmd(streams))
cmd.AddCommand(config.NewCmd(streams))
cmd.AddCommand(helm.NewHelmCmd(streams))

// cmd.Flags().BoolVar(&o.listNamespaces, "list", o.listNamespaces, "if true, print the list of all namespaces in the current KUBECONFIG")
o.configFlags.AddFlags(cmd.Flags())
Expand Down
11 changes: 7 additions & 4 deletions pkg/helmutil/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -36,23 +37,25 @@ func NewUpgrader(c client.Client, repoName, repoURL, chartName string) (*Upgrade
}

// Upgrade installs the missing CRDs or updates them if they exists already
func (u *Upgrader) Upgrade(ctx context.Context, targetVersion string) ([]unstructured.Unstructured, error) {
chartDir, err := GetChartTargetDir(u.chartName, targetVersion)
func (u *Upgrader) Upgrade(ctx context.Context, chartVersion string) ([]unstructured.Unstructured, error) {
chartDir, err := GetChartTargetDir(u.chartName)
if err != nil {
return nil, err
}

if _, err := os.Stat(chartDir); os.IsNotExist(err) {
downloadDir, err := DownloadChartRelease(u.repoName, u.repoURL, u.chartName, targetVersion)
downloadDir, err := DownloadChartRelease(u.repoName, u.repoURL, u.chartName, chartVersion)
if err != nil {
return nil, err
}

extractDir, err := ExtractChartRelease(downloadDir, u.chartName, targetVersion)
extractDir, err := ExtractChartRelease(downloadDir, u.chartName, chartVersion)
if err != nil {
return nil, err
}
chartDir = extractDir
} else {
fmt.Printf("Using cached chart release from %s\n", chartDir)
}

// defer os.RemoveAll(downloadDir)
Expand Down
12 changes: 12 additions & 0 deletions pkg/helmutil/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helmutil_test

import (
"context"
"os"
"strings"
"testing"
"time"
Expand All @@ -20,6 +21,7 @@ func TestUpgradingCRDs(t *testing.T) {
for _, chartName := range chartNames {
namespace := env.CreateNamespace(t)
kubeClient := env.Client(namespace)
require.NoError(cleanCache(chartName))

// creating new upgrader
u, err := helmutil.NewUpgrader(kubeClient, helmutil.K8ssandraRepoName, helmutil.StableK8ssandraRepoURL, chartName)
Expand Down Expand Up @@ -53,6 +55,7 @@ func TestUpgradingCRDs(t *testing.T) {
require.False(strings.HasPrefix(descRunsAsCassandra, "DEPRECATED"))

// Upgrading to 0.46.1
require.NoError(cleanCache(chartName))
_, err = u.Upgrade(context.TODO(), "0.46.1")
require.NoError(err)

Expand All @@ -69,3 +72,12 @@ func TestUpgradingCRDs(t *testing.T) {
require.True(strings.HasPrefix(descRunsAsCassandra, "DEPRECATED"))
}
}

func cleanCache(chartName string) error {
chartDir, err := helmutil.GetChartTargetDir(chartName)
if err != nil {
return err
}

return os.RemoveAll(chartDir)
}
Loading

0 comments on commit 41178b9

Please sign in to comment.