Skip to content

Commit

Permalink
Add stash-crd-installer image to install CRDs on helm pre-install hook (
Browse files Browse the repository at this point in the history
#145)

Signed-off-by: Emruz Hossain <[email protected]>
  • Loading branch information
Emruz Hossain authored Feb 7, 2022
1 parent 9c52874 commit 0906fab
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 26 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.17
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.17
id: go
Expand All @@ -29,6 +29,19 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login Docker
env:
DOCKER_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
USERNAME: 1gtm
run: |
docker login --username ${USERNAME} --password ${DOCKER_TOKEN}
- name: Push Docker Image
env:
REGISTRY: appscodeci
run: |
make push
- name: Run checks
run: |
sudo apt-get -qq update || true
Expand Down
51 changes: 51 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Release

on:
push:
tags:
- "*.*"

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.17
id: go

- name: Print version info
id: semver
run: |
make version
- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Publish to GitHub Container Registry
env:
REGISTRY: ghcr.io/stashed
DOCKER_TOKEN: ${{ secrets.LGTM_GITHUB_TOKEN }}
USERNAME: 1gtm
APPSCODE_ENV: prod
run: |
docker login ghcr.io --username ${USERNAME} --password ${DOCKER_TOKEN}
make release
- name: Publish to Docker Registry
env:
DOCKER_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
USERNAME: 1gtm
APPSCODE_ENV: prod
run: |
docker login --username ${USERNAME} --password ${DOCKER_TOKEN}
make release
2 changes: 1 addition & 1 deletion .github/workflows/update-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.17
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.17
id: go
Expand Down
33 changes: 31 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ GO_PKG := stash.appscode.dev
REPO := $(notdir $(shell pwd))
BIN := apimachinery

# Where to push the docker image.
REGISTRY ?= stashed

# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:generateEmbeddedObjectMeta=true"
CODE_GENERATOR_IMAGE ?= appscode/gengo:release-1.21
Expand Down Expand Up @@ -50,7 +53,7 @@ RESTIC_VER := 0.12.1
### These variables should not need tweaking.
###

SRC_PKGS := apis client crds pkg # directories which hold app source (not vendored)
SRC_PKGS := apis client cmd crds pkg # directories which hold app source (not vendored)
SRC_DIRS := $(SRC_PKGS) hack/gencrd

DOCKER_PLATFORMS := linux/amd64 linux/arm linux/arm64
Expand Down Expand Up @@ -271,7 +274,7 @@ bin/.container-$(DOTFILE_IMAGE)-TEST:
-e 's|{ARG_FROM}|$(BUILD_IMAGE)|g' \
-e 's|{RESTIC_VER}|$(RESTIC_VER)|g' \
$(DOCKERFILE_TEST) > bin/.dockerfile-TEST-$(OS)_$(ARCH)
@DOCKER_CLI_EXPERIMENTAL=enabled docker buildx build --platform $(OS)/$(ARCH) --load --pull -t $(TEST_IMAGE) -f bin/.dockerfile-TEST-$(OS)_$(ARCH) .
@docker buildx build --platform $(OS)/$(ARCH) --load --pull -t $(TEST_IMAGE) -f bin/.dockerfile-TEST-$(OS)_$(ARCH) .
@docker images -q $(TEST_IMAGE) > $@
@echo

Expand Down Expand Up @@ -371,3 +374,29 @@ ci: verify check-license lint build unit-tests #cover
.PHONY: clean
clean:
rm -rf .go bin

.PHONY: push
push: push-crd-installer

KO := $(shell go env GOPATH)/bin/ko
.PHONY: push-crd-installer
push-crd-installer: $(BUILD_DIRS) install-ko ## Build and push CRD installer image
@echo "Pushing CRD installer image....."
KO_DOCKER_REPO=$(REGISTRY) $(KO) publish ./cmd/stash-crd-installer --tags $(VERSION),latest --base-import-paths --platform=all

.PHONY: install-ko
install-ko:
@echo "Installing: github.com/google/ko"
go install github.com/google/ko@latest

.PHONY: release
release: ## Release final production docker image and push into the DockerHub.
@if [ "$$APPSCODE_ENV" != "prod" ]; then \
echo "'release' only works in PROD env."; \
exit 1; \
fi
@if [ "$(version_strategy)" != "tag" ]; then \
echo "apply tag to release binaries and/or docker images."; \
exit 1; \
fi
@$(MAKE) push --no-print-directory
120 changes: 120 additions & 0 deletions cmd/stash-crd-installer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright AppsCode Inc. and Contributors
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 main

import (
"flag"
"os"

stashv1alpha1 "stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
stashv1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"

crd_cs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"kmodules.xyz/client-go/apiextensions"
appcatalog "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
metrics "kmodules.xyz/custom-resources/apis/metrics/v1alpha1"
)

var (
masterURL string
kubeConfig string
enableEnterpriseFeatures bool
)

func init() {
flag.StringVar(&kubeConfig, "kubeconfig", kubeConfig, "Path to a kube config file. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server. Overrides any value in kube config file. Only required if out-of-cluster.")
flag.BoolVar(&enableEnterpriseFeatures, "enterprise", false, "Specify whether enterprise features enabled or not.")
}

func main() {
flag.Parse()

cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeConfig)
if err != nil {
klog.Errorf("Error building kubeconfig: %s", err.Error())
os.Exit(1)
}

crdClient, err := crd_cs.NewForConfig(cfg)
if err != nil {
klog.Errorf("Error building CRD client: %s", err.Error())
os.Exit(1)
}

if err := registerCRDs(crdClient); err != nil {
klog.Errorf("Error building CRD client: %s", err.Error())
os.Exit(1)
}
klog.Infoln("Successfully installed all CRDs.")
}

func registerCRDs(crdClient crd_cs.Interface) error {
var resources []*apiextensions.CustomResourceDefinition

resources = append(resources, getStashCRDs()...)
resources = append(resources, getAppCatalogCRDs()...)

if enableEnterpriseFeatures {
resources = append(resources, getMetricCRDs()...)
}
return apiextensions.RegisterCRDs(crdClient, resources)
}

func getStashCRDs() []*apiextensions.CustomResourceDefinition {
// community features CRDs
var stashCRDs []*apiextensions.CustomResourceDefinition

stashCRDs = append(stashCRDs,
// v1alpha1 resources
stashv1alpha1.Repository{}.CustomResourceDefinition(),

// v1beta1 resources
stashv1beta1.BackupConfiguration{}.CustomResourceDefinition(),
stashv1beta1.BackupSession{}.CustomResourceDefinition(),
stashv1beta1.RestoreSession{}.CustomResourceDefinition(),
stashv1beta1.Function{}.CustomResourceDefinition(),
stashv1beta1.Task{}.CustomResourceDefinition(),
)

// enterprise features CRDs
if enableEnterpriseFeatures {
stashCRDs = append(stashCRDs,
// v1beta1 resources
stashv1beta1.BackupBatch{}.CustomResourceDefinition(),
stashv1beta1.BackupBlueprint{}.CustomResourceDefinition(),
stashv1beta1.RestoreBatch{}.CustomResourceDefinition(),
)
}
return stashCRDs
}

func getAppCatalogCRDs() []*apiextensions.CustomResourceDefinition {
return []*apiextensions.CustomResourceDefinition{
// v1alpha1 resources
appcatalog.AppBinding{}.CustomResourceDefinition(),
}
}

func getMetricCRDs() []*apiextensions.CustomResourceDefinition {
return []*apiextensions.CustomResourceDefinition{
// v1alpha1 resources
metrics.MetricsConfiguration{}.CustomResourceDefinition(),
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
gomodules.xyz/pointer v0.1.0
gomodules.xyz/runtime v0.2.0
k8s.io/api v0.21.1
k8s.io/apiextensions-apiserver v0.21.1
k8s.io/apimachinery v0.21.1
k8s.io/client-go v0.21.1
k8s.io/klog/v2 v2.8.0
Expand Down
8 changes: 4 additions & 4 deletions hack/gencrd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func generateSwaggerJson() {

// v1beta1 resources
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralBackupConfiguration, stashv1beta1.ResourceKindBackupConfiguration, true},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourceKindBackupSession, stashv1beta1.ResourceKindBackupSession, true},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourceKindBackupBatch, stashv1beta1.ResourceKindBackupBatch, false},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourceKindBackupBlueprint, stashv1beta1.ResourceKindBackupBlueprint, false},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralBackupSession, stashv1beta1.ResourceKindBackupSession, true},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralBackupBatch, stashv1beta1.ResourceKindBackupBatch, false},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralBackupBlueprint, stashv1beta1.ResourceKindBackupBlueprint, false},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralRestoreSession, stashv1beta1.ResourceKindRestoreSession, true},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralRestoreBatch, stashv1beta1.ResourceKindRestoreBatch, true},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourceKindFunction, stashv1beta1.ResourceKindFunction, false},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralFunction, stashv1beta1.ResourceKindFunction, false},
{stashv1beta1.SchemeGroupVersion, stashv1beta1.ResourcePluralTask, stashv1beta1.ResourceKindTask, false},
},
//nolint:govet
Expand Down
Loading

0 comments on commit 0906fab

Please sign in to comment.