-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Micah Hausler <[email protected]> ## Description This PR adds a new Kubernetes controller binary for the types merged in #542. * Adds a new `cmd/tink-controller` package with a Dockerfile and a `main.go` * Removes the `status.data` field on Workflows. This was just a string of the interpolated template, and was redundant given the strongly typed template actions already on the `.status` of Workflows. * Adds tests to the methods on the workflow API structure * Adds a new `pkg/controllers` package. This introduces a `Manager` abstraction that wraps `sigs.k8s.io/controller-runtime`'s [`Reconciler`](https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile?utm_source=gopls#Reconciler) type * Controllers for individual types (workflows, templates, hardware) will reside in individual sub-packages * Controllers patch incoming objects even in the event of an error: this is intentional as a controller in the [Kubernetes Resource Model](https://github.com/kubernetes/design-proposals-archive/blob/main/architecture/resource-management.md) attempts to converge on desired state * Adds a new `pkg/conversion` package for converting protobuf types to Kubernetes CRD types and vice versa * Adds an internal `pkg/internal/tests` package with internal testing utilities. * The first utility added is a `FrozenTime` interface with Before/After connivence methods for `time.Time`, as well as protobuf and K8s metav1 package time formats. ## How Has This Been Tested? * I added unit tests for nearly all new branching logic * I added a CI target for building the tink-controller ## How are existing users impacted? What migration steps/scripts do we need? There is no impact to existing users. Nothing form this change is called in any existing code path. ## Checklist: I have: - [ ] ~~updated the documentation and/or roadmap (if required)~~ - [x] added unit or e2e tests - [ ] ~~provided instructions on how to upgrade~~
- Loading branch information
Showing
31 changed files
with
2,703 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tink-controller | ||
tink-controller-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM alpine:3.15 | ||
ENTRYPOINT ["/usr/bin/tink-controller"] | ||
EXPOSE 42113 | ||
EXPOSE 42114 | ||
|
||
ARG TARGETARCH | ||
ARG TARGETVARIANT | ||
|
||
RUN apk add --update ca-certificates | ||
COPY tink-controller-linux-${TARGETARCH:-amd64}${TARGETVARIANT} /usr/bin/tink-controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.