Skip to content

Commit

Permalink
Added Kubernetes Controller
Browse files Browse the repository at this point in the history
Signed-off-by: Micah Hausler <[email protected]>
  • Loading branch information
micahhausler committed Dec 3, 2021
1 parent 058a1c9 commit b29b9ea
Show file tree
Hide file tree
Showing 33 changed files with 2,678 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{Makefile,go.mod,go.sum,*.go,.gitmodules,*.sh}]
[{Makefile,go.mod,go.sum,*.go,.gitmodules,*.sh,*.mk}]
indent_size = 8
indent_style = tab

Expand Down
11 changes: 9 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.14.6"
go-version: "1.17.0"
- name: Generate
run: make generate
- name: go test
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.14.6"
go-version: "1.17.0"
- run: make crosscompile -j$(nproc)
- name: Upload tink-cli binaries
uses: actions/upload-artifact@v2
Expand All @@ -77,6 +77,11 @@ jobs:
with:
name: tink-worker
path: cmd/tink-worker/tink-worker-*
- name: Upload tink-controller binaries
uses: actions/upload-artifact@v2
with:
name: tink-controller
path: cmd/tink-controller/tink-controller-*
docker-images:
runs-on: ubuntu-latest
needs:
Expand All @@ -90,6 +95,8 @@ jobs:
binary: tink-server
- repository: quay.io/tinkerbell/tink-worker
binary: tink-worker
- repository: quay.io/tinkerbell/tink-controller
binary: tink-controller
steps:
- name: Docker Image Tag for Sha
id: docker-image-tag
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bin/
certs/
cmd/tink-cli/tink-cli
cmd/tink-controller/tink-controller
cmd/tink-server/tink-server
cmd/tink-worker/tink-worker
doc/
Expand Down
2 changes: 2 additions & 0 deletions cmd/tink-controller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tink-controller
tink-controller-*
11 changes: 11 additions & 0 deletions cmd/tink-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM alpine:3.15
ENTRYPOINT ["/usr/bin/tink-controller"]
EXPOSE 42113
EXPOSE 42114

ARG TARGETARCH
ARG TARGETVARIANT

RUN apk add --update ca-certificates && \
apk add --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing cfssl
COPY tink-controller-linux-${TARGETARCH:-amd64}${TARGETVARIANT} /usr/bin/tink-controller
125 changes: 125 additions & 0 deletions cmd/tink-controller/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"context"
"fmt"
"os"
"strings"

"github.com/packethost/pkg/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tinkerbell/tink/pkg/controllers"
wfctrl "github.com/tinkerbell/tink/pkg/controllers/workflow"
"k8s.io/client-go/tools/clientcmd"
)

// version is set at build time.
var version = "devel"

// DaemonConfig represents all the values you can configure as part of the tink-server.
// You can change the configuration via environment variable, or file, or command flags.
type DaemonConfig struct {
K8sAPI string
Kubeconfig string // only applies to out of cluster
}

func (c *DaemonConfig) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.K8sAPI, "kubernetes", "", "The Kubernetes URL")
fs.StringVar(&c.Kubeconfig, "kubeconfig", "", "Absolute path to the kubeconfig file")
}

func main() {
logger, err := log.Init("github.com/tinkerbell/tink")
if err != nil {
panic(err)
}
defer logger.Close()

config := &DaemonConfig{}

cmd := NewRootCommand(config, logger)
if err := cmd.ExecuteContext(context.Background()); err != nil {
defer os.Exit(1)
}
}

func NewRootCommand(config *DaemonConfig, logger log.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "tink-controller",
PreRunE: func(cmd *cobra.Command, args []string) error {
viper, err := createViper(logger)
if err != nil {
return err
}
return applyViper(viper, cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
logger.Info("starting controller version " + version)

config, err := clientcmd.BuildConfigFromFlags(config.K8sAPI, config.Kubeconfig)
if err != nil {
return err
}

manager, err := controllers.NewManager(config, controllers.GetControllerOptions())
if err != nil {
return err
}

return manager.RegisterControllers(
cmd.Context(),
wfctrl.NewController(manager.GetClient()),
).Start(cmd.Context())
},
}
config.AddFlags(cmd.Flags())
return cmd
}

func createViper(logger log.Logger) (*viper.Viper, error) {
v := viper.New()
v.AutomaticEnv()
v.SetConfigName("tink-controller")
v.AddConfigPath("/etc/tinkerbell")
v.AddConfigPath(".")
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// If a config file is found, read it in.
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
logger.With("configFile", v.ConfigFileUsed()).Error(err, "could not load config file")
return nil, err
}
logger.Info("no config file found")
} else {
logger.With("configFile", v.ConfigFileUsed()).Info("loaded config file")
}

return v, nil
}

func applyViper(v *viper.Viper, cmd *cobra.Command) error {
errors := []error{}

cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
errors = append(errors, err)
return
}
}
})

if len(errors) > 0 {
errs := []string{}
for _, err := range errors {
errs = append(errs, err.Error())
}
return fmt.Errorf(strings.Join(errs, ", "))
}

return nil
}
3 changes: 0 additions & 3 deletions config/crd/bases/tinkerbell.org_workflowdata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ spec:
status:
description: WorkflowStatus defines the observed state of Workflow.
properties:
data:
description: Data is the populated Workflow Data in Tinkerbell.
type: string
globalTimeout:
description: GlobalTimeout represents the max execution time
format: int64
Expand Down
3 changes: 0 additions & 3 deletions config/crd/bases/tinkerbell.org_workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ spec:
status:
description: WorkflowStatus defines the observed state of Workflow.
properties:
data:
description: Data is the populated Workflow Data in Tinkerbell.
type: string
globalTimeout:
description: GlobalTimeout represents the max execution time
format: int64
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ require (
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v20.10.7+incompatible
github.com/equinix-labs/otel-init-go v0.0.1
github.com/go-logr/zapr v0.4.0
github.com/go-openapi/strfmt v0.19.3 // indirect
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.2.0
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
Expand All @@ -30,12 +31,15 @@ require (
github.com/testcontainers/testcontainers-go v0.11.1
go.mongodb.org/mongo-driver v1.1.2 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.22.0
google.golang.org/genproto v0.0.0-20210921142501-181ce0d877f6
google.golang.org/grpc v1.41.0-dev.0.20210907181116-2f3355d2244e
go.uber.org/multierr v1.7.0
google.golang.org/genproto v0.0.0-20211021150943-2b146023228c
google.golang.org/grpc v1.42.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
knative.dev/pkg v0.0.0-20211119170723-a99300deff34
mvdan.cc/gofumpt v0.1.1
sigs.k8s.io/controller-runtime v0.10.1
)
Expand Down
Loading

0 comments on commit b29b9ea

Please sign in to comment.