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 2, 2021
1 parent 058a1c9 commit 3414844
Show file tree
Hide file tree
Showing 31 changed files with 2,968 additions and 592 deletions.
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
}
Loading

0 comments on commit 3414844

Please sign in to comment.