Skip to content

Commit

Permalink
Clean up and rearrange packages
Browse files Browse the repository at this point in the history
- Remove TLS configurability
- Move packages that can be easily moved and are internal to /internal
- Update some package names to be more representative of their function
- Remove obsolete/unused code

Signed-off-by: Chris Doherty <[email protected]>
  • Loading branch information
chrisdoherty4 committed Dec 27, 2022
1 parent 1369125 commit 8ccc1d6
Show file tree
Hide file tree
Showing 39 changed files with 69 additions and 366 deletions.
89 changes: 0 additions & 89 deletions client/main.go

This file was deleted.

36 changes: 4 additions & 32 deletions cmd/tink-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

Expand All @@ -15,12 +14,10 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
grpcserver "github.com/tinkerbell/tink/grpc-server"
httpserver "github.com/tinkerbell/tink/http-server"
"github.com/tinkerbell/tink/metrics"
"github.com/tinkerbell/tink/server"
"github.com/tinkerbell/tink/internal/grpcserver"
"github.com/tinkerbell/tink/internal/httpserver"
"github.com/tinkerbell/tink/internal/server"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

// version is set at build time.
Expand All @@ -30,15 +27,8 @@ var version = "devel"
// You can change the configuration via environment variable, or file, or command flags.
type DaemonConfig struct {
Facility string
PGDatabase string
PGUSer string
PGPassword string
PGSSLMode string
OnlyMigration bool
GRPCAuthority string
CertDir string
HTTPAuthority string
TLS bool
Backend string

KubeconfigPath string
Expand All @@ -55,9 +45,7 @@ func backends() []string {
func (c *DaemonConfig) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.Facility, "facility", "deprecated", "This is temporary. It will be removed")
fs.StringVar(&c.GRPCAuthority, "grpc-authority", ":42113", "The address used to expose the gRPC server")
fs.StringVar(&c.CertDir, "cert-dir", "", "")
fs.StringVar(&c.HTTPAuthority, "http-authority", ":42114", "The address used to expose the HTTP server")
fs.BoolVar(&c.TLS, "tls", true, "Run in tls protected mode (disabling should only be done for development or if behind TLS terminating proxy)")
fs.StringVar(&c.Backend, "backend", backendKubernetes, fmt.Sprintf("The backend datastore to use. Must be one of %s", strings.Join(backends(), ", ")))
fs.StringVar(&c.KubeconfigPath, "kubeconfig", "", "The path to the Kubeconfig. Only takes effect if `--backend=kubernetes`")
fs.StringVar(&c.KubeAPI, "kubernetes", "", "The Kubernetes API URL, used for in-cluster client construction. Only takes effect if `--backend=kubernetes`")
Expand All @@ -66,11 +54,8 @@ func (c *DaemonConfig) AddFlags(fs *pflag.FlagSet) {

func (c *DaemonConfig) PopulateFromLegacyEnvVar() {
c.Facility = env.Get("FACILITY", c.Facility)

c.CertDir = env.Get("TINKERBELL_CERTS_DIR", c.CertDir)
c.GRPCAuthority = env.Get("TINKERBELL_GRPC_AUTHORITY", c.GRPCAuthority)
c.HTTPAuthority = env.Get("TINKERBELL_HTTP_AUTHORITY", c.HTTPAuthority)
c.TLS = env.Bool("TINKERBELL_TLS", c.TLS)
}

func main() {
Expand Down Expand Up @@ -110,7 +95,6 @@ func NewRootCommand(config *DaemonConfig, logger log.Logger) *cobra.Command {
// the most aggressive way we have to guarantee that
// the old way works as before.
config.PopulateFromLegacyEnvVar()
metrics.SetupMetrics(config.Facility, logger)

logger.Info("starting version " + version)

Expand All @@ -124,20 +108,8 @@ func NewRootCommand(config *DaemonConfig, logger log.Logger) *cobra.Command {
errCh := make(chan error, 2)
var (
registrar grpcserver.Registrar
grpcOpts []grpc.ServerOption
err error
)
if config.TLS {
certDir := config.CertDir
if certDir == "" {
certDir = env.Get("TINKERBELL_CERTS_DIR", filepath.Join("/certs", config.Facility))
}
cert, err := grpcserver.GetCerts(certDir)
if err != nil {
return err
}
grpcOpts = append(grpcOpts, grpc.Creds(credentials.NewServerTLSFromCert(cert)))
}
switch config.Backend {
case backendKubernetes:
var err error
Expand All @@ -158,7 +130,7 @@ func NewRootCommand(config *DaemonConfig, logger log.Logger) *cobra.Command {
ctx,
registrar,
config.GRPCAuthority,
grpcOpts,
[]grpc.ServerOption{},
errCh)
if err != nil {
return err
Expand Down
8 changes: 2 additions & 6 deletions cmd/tink-worker/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/cmd/tink-worker/worker"
"github.com/tinkerbell/tink/internal/client"
"github.com/tinkerbell/tink/protos/workflow"
)

Expand Down Expand Up @@ -44,10 +44,7 @@ func NewRootCommand(version string, logger log.Logger) *cobra.Command {

logger.With("version", version).Info("starting")

conn, err := client.NewClientConn(
viper.GetString("tinkerbell-grpc-authority"),
viper.GetBool("tinkerbell-tls"),
)
conn, err := client.NewClientConn(viper.GetString("tinkerbell-grpc-authority"))
if err != nil {
return err
}
Expand Down Expand Up @@ -92,7 +89,6 @@ func NewRootCommand(version string, logger log.Logger) *cobra.Command {
rootCmd.Flags().Int("max-retry", defaultRetryCount, "Maximum number of retries to attempt (MAX_RETRY)")
rootCmd.Flags().Int64("max-file-size", defaultMaxFileSize, "Maximum file size in bytes (MAX_FILE_SIZE)")
rootCmd.Flags().Bool("capture-action-logs", true, "Capture action container output as part of worker logs")
rootCmd.Flags().Bool("tinkerbell-tls", true, "Connect to server via TLS or not (TINKERBELL_TLS)")
rootCmd.Flags().StringP("docker-registry", "r", "", "Sets the Docker registry (DOCKER_REGISTRY)")
rootCmd.Flags().StringP("registry-username", "u", "", "Sets the registry username (REGISTRY_USERNAME)")
rootCmd.Flags().StringP("registry-password", "p", "", "Sets the registry-password (REGISTRY_PASSWORD)")
Expand Down
7 changes: 2 additions & 5 deletions cmd/virtual-worker/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tinkerbell/tink/client"
tinkWorker "github.com/tinkerbell/tink/cmd/tink-worker/worker"
"github.com/tinkerbell/tink/cmd/virtual-worker/worker"
"github.com/tinkerbell/tink/internal/client"
"github.com/tinkerbell/tink/protos/workflow"
)

Expand Down Expand Up @@ -40,10 +40,7 @@ func NewRootCommand(version string, logger log.Logger) *cobra.Command {

logger.With("version", version).Info("starting")

conn, err := client.NewClientConn(
viper.GetString("tinkerbell-grpc-authority"),
viper.GetBool("tinkerbell-tls"),
)
conn, err := client.NewClientConn(viper.GetString("tinkerbell-grpc-authority"))
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion config/server/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ spec:
containers:
- args:
- "--backend=kubernetes"
- "--tls=false"
image: server:latest
imagePullPolicy: IfNotPresent
name: tink-server
Expand Down
35 changes: 0 additions & 35 deletions docs/ENVVARS.md

This file was deleted.

20 changes: 20 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func NewClientConn(authority string) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(authority,
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, errors.Wrap(err, "connect to tinkerbell server")
}
return conn, nil
}
2 changes: 1 addition & 1 deletion pkg/convert/workflow.go → internal/convert/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"sort"

"github.com/tinkerbell/tink/internal/workflow"
"github.com/tinkerbell/tink/pkg/apis/core/v1alpha1"
protoworkflow "github.com/tinkerbell/tink/protos/workflow"
"github.com/tinkerbell/tink/workflow"
)

func WorkflowToWorkflowContext(wf *v1alpha1.Workflow) *protoworkflow.WorkflowContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tinkerbell/tink/internal/tests"
"github.com/tinkerbell/tink/internal/testtime"
"github.com/tinkerbell/tink/internal/workflow"
"github.com/tinkerbell/tink/pkg/apis/core/v1alpha1"
protoworkflow "github.com/tinkerbell/tink/protos/workflow"
"github.com/tinkerbell/tink/workflow"
"google.golang.org/protobuf/testing/protocmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var TestTime = tests.NewFrozenTimeUnix(1637361794)
var TestTime = testtime.NewFrozenTimeUnix(1637361794)

func TestWorkflowToWorkflowContext(t *testing.T) {
cases := []struct {
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e_test.go → internal/e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests_test
package e2e_test

import (
"context"
Expand All @@ -9,9 +9,9 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/cmd/tink-worker/worker"
virtWorker "github.com/tinkerbell/tink/cmd/virtual-worker/worker"
"github.com/tinkerbell/tink/internal/client"
"github.com/tinkerbell/tink/pkg/apis/core/v1alpha1"
pb "github.com/tinkerbell/tink/protos/workflow"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -84,7 +84,7 @@ var _ = Describe("Tink API", func() {
}, timeout, interval).Should(Equal("STATE_PENDING"))

By("Running a virtual worker")
conn, err := client.NewClientConn(serverAddr, false)
conn, err := client.NewClientConn(serverAddr)
Expect(err).NotTo(HaveOccurred())
rClient := pb.NewWorkflowServiceClient(conn)

Expand Down Expand Up @@ -154,7 +154,7 @@ var _ = Describe("Tink API", func() {
}, timeout, interval).Should(Equal("STATE_PENDING"))

By("Getting Workflow Contexts")
conn, err := client.NewClientConn(serverAddr, false)
conn, err := client.NewClientConn(serverAddr)
Expect(err).NotTo(HaveOccurred())
rClient := pb.NewWorkflowServiceClient(conn)
workerID := hardware.Spec.Interfaces[0].DHCP.MAC
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8ccc1d6

Please sign in to comment.