Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable running the TUF server outside of k8s #1159

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions cmd/tuf/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,42 @@ var (
// root = Which holds 1.root.json
// repository - Compressed repo, which has been tar/gzipped.
secretName = flag.String("rootsecret", "tuf-root", "Name of the secret to create for the initial root file")
noK8s = flag.Bool("no-k8s", false, "Run in a non-k8s environment")
)

func main() {
flag.Parse()
func getNamespaceAndClientset(noK8s bool) (string, *kubernetes.Clientset, error) {
if noK8s {
return "", nil, nil
}

ns := os.Getenv("NAMESPACE")
if ns == "" {
panic("env variable NAMESPACE must be set")
}
ctx := signals.NewContext()

versionInfo := version.GetVersionInfo()
logging.FromContext(ctx).Infof("running create_repo Version: %s GitCommit: %s BuildDate: %s", versionInfo.GitVersion, versionInfo.GitCommit, versionInfo.BuildDate)

config, err := rest.InClusterConfig()
if err != nil {
logging.FromContext(ctx).Panicf("Failed to get InClusterConfig: %v", err)
return "", nil, fmt.Errorf("Failed to get InClusterConfig: %v", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logging.FromContext(ctx).Panicf("Failed to get clientset: %v", err)
return "", nil, fmt.Errorf("Failed to get clientset: %v", err)
}

return ns, clientset, nil
}

func main() {
flag.Parse()

ctx := signals.NewContext()

versionInfo := version.GetVersionInfo()
logging.FromContext(ctx).Infof("running create_repo Version: %s GitCommit: %s BuildDate: %s", versionInfo.GitVersion, versionInfo.GitCommit, versionInfo.BuildDate)

ns, clientset, err := getNamespaceAndClientset(*noK8s)
if err != nil {
logging.FromContext(ctx).Panicf("Failed to get namespace and clientset: %v", err)
}

tufFiles, err := os.ReadDir(*dir)
Expand Down Expand Up @@ -129,10 +144,13 @@ func main() {
}
data["repository"] = compressed.Bytes()

nsSecret := clientset.CoreV1().Secrets(ns)
if err := secret.ReconcileSecret(ctx, *secretName, ns, data, nsSecret); err != nil {
logging.FromContext(ctx).Panicf("Failed to reconcile secret %s/%s: %v", ns, *secretName, err)
if !*noK8s {
nsSecret := clientset.CoreV1().Secrets(ns)
if err := secret.ReconcileSecret(ctx, *secretName, ns, data, nsSecret); err != nil {
logging.FromContext(ctx).Panicf("Failed to reconcile secret %s/%s: %v", ns, *secretName, err)
}
}

// Serve the TUF repository.
logging.FromContext(ctx).Infof("tuf repository was created in: %s", dir)
serveDir := filepath.Join(dir, "repository")
Expand Down
Loading