Skip to content

Commit

Permalink
refactor: merge cert-generator to karpor image (#580)
Browse files Browse the repository at this point in the history
## What type of PR is this?

/kind refactor

## What this PR does / why we need it:

- Merge cert-generator to karpor image.
- Support `cert-generator --version`.
- Refact `karpor --version` implement.
- Support to publish image of arm64 platform.

## Which issue(s) this PR fixes:

<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related
issues/tests in a comment and do not use `Fixes`_*
-->

Fixes #
  • Loading branch information
elliotxx authored Aug 7, 2024
1 parent cafe1c5 commit 8a60e6a
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 873 deletions.
29 changes: 27 additions & 2 deletions .goreleaser/.goreleaser-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ before:
- make gen-version

builds:
- env:
- id: "karpor"
env:
- CGO_ENABLED=0
# GOOS list to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
Expand All @@ -42,7 +43,31 @@ builds:
# By using ldflags="-s -w", the symbol table and debug information are removed to decrease the size of the release package.
ldflags:
- -s -w
main: ./cmd/
main: ./cmd/karpor/
binary: karpor
- id: "cert-generator"
env:
- CGO_ENABLED=0
# GOOS list to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
#
# Default: [ 'darwin', 'linux', 'windows' ]
goos:
- linux
# - windows
- darwin
# GOARCH to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
#
# Default: [ '386', 'amd64', 'arm64' ]
goarch:
- amd64
- arm64
# By using ldflags="-s -w", the symbol table and debug information are removed to decrease the size of the release package.
ldflags:
- -s -w
main: ./cmd/cert-generator/
binary: cert-generator

release:
disable: true
Expand Down
85 changes: 65 additions & 20 deletions .goreleaser/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ before:
- go mod tidy
- make gen-version
builds:
- env:
- id: "karpor"
env:
- CGO_ENABLED=0
# GOOS list to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
Expand All @@ -41,10 +42,37 @@ builds:
# By using ldflags="-s -w", the symbol table and debug information are removed to decrease the size of the release package.
ldflags:
- -s -w
main: ./cmd/
main: ./cmd/karpor/
binary: karpor
- id: "cert-generator"
env:
- CGO_ENABLED=0
# GOOS list to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
#
# Default: [ 'darwin', 'linux', 'windows' ]
goos:
- linux
# - windows
- darwin
# GOARCH to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
#
# Default: [ '386', 'amd64', 'arm64' ]
goarch:
- amd64
- arm64
# By using ldflags="-s -w", the symbol table and debug information are removed to decrease the size of the release package.
ldflags:
- -s -w
main: ./cmd/cert-generator/
binary: cert-generator
archives:
- id: release
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
builds:
- karpor
- cert-generator
builds_info:
mode: 0644
# format is `time.RFC3339Nano`
Expand All @@ -56,6 +84,8 @@ archives:
- LICENSE
- README.md
- docs/*
- src: pkg/version/VERSION
strip_parent: true
- src: dist/CHANGELOG.md
strip_parent: true
checksum:
Expand Down Expand Up @@ -123,10 +153,13 @@ release:
## Thanks!
prerelease: auto


# Reference:
# - https://blog.goreleaser.com/goreleaser-and-docker-manifests-9fe167acf21e
# - https://goreleaser.com/cookbooks/multi-platform-docker-images/#creating-multi-platform-docker-images-with-goreleaser
dockers:
- image_templates:
- 'kusionstack/{{ .ProjectName }}:{{ .Tag }}'
- 'kusionstack/{{ .ProjectName }}:latest'
- 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-amd64'
dockerfile: Dockerfile
use: docker
build_flag_templates:
Expand All @@ -137,21 +170,33 @@ dockers:
- "--label=org.opencontainers.image.version={{.Version}}"
- "--label=org.opencontainers.image.source={{.GitURL}}"
- "--platform=linux/amd64"
goarch: amd64
extra_files:
- ui/
- pkg/version/VERSION
- config/relationship.yaml
# - image_templates:
# - 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64'
# dockerfile: Dockerfile
# use: docker
# build_flag_templates:
# - "--pull"
# - "--label=org.opencontainers.image.created={{.Date}}"
# - "--label=org.opencontainers.image.name={{.ProjectName}}"
# - "--label=org.opencontainers.image.revision={{.FullCommit}}"
# - "--label=org.opencontainers.image.version={{.Version}}"
# - "--label=org.opencontainers.image.source={{.GitURL}}"
# - "--platform=linux/arm64"
# goarch: arm64
# extra_files:
# - ui/
- image_templates:
- 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64'
dockerfile: Dockerfile
use: docker
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.name={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--label=org.opencontainers.image.source={{.GitURL}}"
- "--platform=linux/arm64"
goarch: arm64
extra_files:
- pkg/version/VERSION
- config/relationship.yaml

docker_manifests:
- name_template: "kusionstack/{{ .ProjectName }}:{{ .Tag }}"
image_templates:
- "kusionstack/{{ .ProjectName }}:{{ .Tag }}-amd64"
- "kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64"
- name_template: "kusionstack/{{ .ProjectName }}:latest"
image_templates:
- "kusionstack/{{ .ProjectName }}:{{ .Tag }}-amd64"
- "kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64"
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ USER root
WORKDIR /

COPY karpor .
COPY cert-generator .
COPY config/relationship.yaml .
COPY pkg/version/VERSION .

ENTRYPOINT ["/karpor"]
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ build-darwin: gen-version $(BUILD_UI) ## Build for MacOS (Darwin)
@echo "🚀 Building karpor-server for darwin platform ..."
GOOS=darwin GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
go build -o ./_build/darwin/$(APPROOT) \
./cmd
./cmd/karpor

# Target: build-linux
# Description: Builds for Linux platform.
Expand All @@ -110,7 +110,7 @@ build-linux: gen-version $(BUILD_UI) ## Build for Linux
@echo "🚀 Building karpor-server for linux platform ..."
GOOS=linux GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
go build -o ./_build/linux/$(APPROOT) \
./cmd
./cmd/karpor

# Target: build-windows
# Description: Builds for Windows platform.
Expand All @@ -126,7 +126,7 @@ build-windows: gen-version $(BUILD_UI) ## Build for Windows
@echo "🚀 Building karpor-server for windows platform ..."
GOOS=windows GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
go build -o ./_build/windows/$(APPROOT).exe \
./cmd
./cmd/karpor

# Target: build-ui
# Description: Builds the UI for the dashboard.
Expand Down
4 changes: 3 additions & 1 deletion cmd/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# `/cmd`

Main applications for `karpor`.
All applications for `karpor`, including:
- Main application `karpor`, the `karpor-server` and `karpor-syncer` both start through it.
- Cert generator `cert-generator`.

It's common to have a small `main` function that imports and invokes the code from the `/pkg` directories and nothing else.

Expand Down
13 changes: 10 additions & 3 deletions hack/cert-generator/main.go → cmd/cert-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package main

import (
"context"
"fmt"
"os"

"github.com/KusionStack/karpor/hack/cert-generator/generator"
"github.com/KusionStack/karpor/pkg/util/certgenerator"
"github.com/KusionStack/karpor/pkg/version"
"github.com/spf13/cobra"
"k8s.io/apiserver/pkg/server"
"k8s.io/client-go/rest"
Expand All @@ -36,7 +38,7 @@ func main() {
func NewCertGeneratorCommand(ctx context.Context) *cobra.Command {
options := NewCertOptions()
cmd := &cobra.Command{
Use: "gen-cert",
Use: "cert-generator",
Short: "Generate CA and kubeconfig",
RunE: func(cmd *cobra.Command, args []string) error {
return runCertGenerator(ctx, options)
Expand All @@ -51,6 +53,11 @@ func runCertGenerator(ctx context.Context, options *CertOptions) error {
var ns string
var err error

if options.Version {
fmt.Println(version.GetVersion())
return nil
}

if options.KubeConfig == "" {
cfg, err = rest.InClusterConfig()
if err != nil {
Expand All @@ -74,7 +81,7 @@ func runCertGenerator(ctx context.Context, options *CertOptions) error {
ns = options.Namespace
}

generator, err := generator.NewGenerator(cfg, ns, options.CertName, options.KubeConfigName)
generator, err := certgenerator.NewGenerator(cfg, ns, options.CertName, options.KubeConfigName)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type CertOptions struct {
Namespace string
CertName string
KubeConfigName string
Version bool
}

func NewCertOptions() *CertOptions {
Expand All @@ -43,4 +44,5 @@ func (o *CertOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Namespace, "namespace", o.Namespace, "The namespace to store the CA and kubeconfig")
fs.StringVar(&o.CertName, "ca-name", o.CertName, "The name of the secret used to store the CA certificate.")
fs.StringVar(&o.KubeConfigName, "kubeconfig-name", o.KubeConfigName, "The name of the configmap used to store the kubeconfig.")
fs.BoolVarP(&o.Version, "version", "V", o.Version, "Print version and exit")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
type CoreOptions struct {
ReadOnlyMode bool
GithubBadge bool
Version bool
}

func NewCoreOptions() *CoreOptions {
Expand All @@ -46,4 +47,5 @@ func (o *CoreOptions) AddFlags(fs *pflag.FlagSet) {

fs.BoolVar(&o.ReadOnlyMode, "read-only-mode", false, "turn on the read only mode")
fs.BoolVar(&o.GithubBadge, "github-badge", false, "whether to display the github badge")
fs.BoolVarP(&o.Version, "version", "V", o.Version, "Print version and exit")
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 3 additions & 6 deletions cmd/app/server.go → cmd/karpor/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"net/http"
"os"

"github.com/KusionStack/karpor/cmd/app/options"
"github.com/KusionStack/karpor/cmd/karpor/app/options"
"github.com/KusionStack/karpor/pkg/kubernetes/registry"
"github.com/KusionStack/karpor/pkg/kubernetes/scheme"
"github.com/KusionStack/karpor/pkg/server"
Expand Down Expand Up @@ -106,8 +106,8 @@ func NewServerCommand(ctx context.Context) *cobra.Command {
Short: "Launch an API server",
Long: "Launch an API server",
RunE: func(c *cobra.Command, args []string) error {
if versionFlag, _ := c.Flags().GetBool("version"); versionFlag {
fmt.Println("Karpor version:", version.GetVersion())
if o.CoreOptions.Version {
fmt.Println(version.GetVersion())
return nil
}
if err := o.Complete(); err != nil {
Expand All @@ -123,9 +123,6 @@ func NewServerCommand(ctx context.Context) *cobra.Command {
},
}

// Add version flag
cmd.Flags().BoolP("version", "V", false, "Print version and exit")

o.AddFlags(cmd.Flags())
return cmd
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/main.go → cmd/karpor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"os"

"github.com/KusionStack/karpor/cmd/app"
"github.com/KusionStack/karpor/cmd/karpor/app"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/cli"
)
Expand Down
Loading

0 comments on commit 8a60e6a

Please sign in to comment.