|
| 1 | +/* |
| 2 | +Copyright 2025 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "os" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "github.com/tektoncd/triggers/pkg/bootstrap" |
| 27 | + "k8s.io/client-go/rest" |
| 28 | + "k8s.io/client-go/tools/clientcmd" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + // Bootstrap flags |
| 33 | + kubeconfig string |
| 34 | + |
| 35 | + // GitHub integration flags |
| 36 | + githubRepo string |
| 37 | + githubToken string |
| 38 | + publicDomain string |
| 39 | + webhookSecret string |
| 40 | +) |
| 41 | + |
| 42 | +// bootstrapCmd represents the bootstrap command |
| 43 | +var bootstrapCmd = &cobra.Command{ |
| 44 | + Use: "bootstrap", |
| 45 | + Short: "Bootstrap Tekton Triggers with all necessary resources", |
| 46 | + Long: `Bootstrap sets up Tekton Triggers with all necessary resources following |
| 47 | +the getting-started guide. This includes: |
| 48 | +
|
| 49 | +- Installing Tekton Pipelines (if not present) |
| 50 | +- Installing Tekton Triggers (if not present) |
| 51 | +- Creating the getting-started namespace |
| 52 | +- Setting up RBAC (ServiceAccounts, Roles, RoleBindings) |
| 53 | +- Creating EventListener, TriggerTemplate, TriggerBinding |
| 54 | +- Creating example Pipeline and Tasks |
| 55 | +
|
| 56 | +Examples: |
| 57 | + # Basic bootstrap (auto-detects kubeconfig, creates all resources) |
| 58 | + tkn triggers bootstrap`, |
| 59 | + RunE: bootstrapRun, |
| 60 | +} |
| 61 | + |
| 62 | +func init() { |
| 63 | + bootstrapCmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "Path to kubeconfig file") |
| 64 | + bootstrapCmd.Flags().StringVar(&githubRepo, "github-repo", "", "GitHub repository (owner/repo)") |
| 65 | + bootstrapCmd.Flags().StringVar(&githubToken, "github-token", "", "GitHub personal access token") |
| 66 | + bootstrapCmd.Flags().StringVar(&publicDomain, "domain", "", "Public domain for webhooks (e.g. myapp.com)") |
| 67 | + bootstrapCmd.Flags().StringVar(&webhookSecret, "webhook-secret", "", "Webhook secret (auto-generated if empty)") |
| 68 | +} |
| 69 | + |
| 70 | +func bootstrapRun(cmd *cobra.Command, args []string) error { |
| 71 | + ctx := context.Background() |
| 72 | + |
| 73 | + // Create kubernetes config |
| 74 | + var config *rest.Config |
| 75 | + var err error |
| 76 | + if kubeconfig != "" { |
| 77 | + // Use kubeconfig |
| 78 | + config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) |
| 79 | + } else { |
| 80 | + // try ~/.kube/config first, then in-cluster |
| 81 | + kubeconfigPath := os.Getenv("HOME") + "/.kube/config" |
| 82 | + config, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath) |
| 83 | + if err != nil { |
| 84 | + config, err = rest.InClusterConfig() |
| 85 | + } |
| 86 | + } |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to build kubeconfig: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Create bootstrap configuration for getting-started resources |
| 92 | + log.Println("== Setting up Tekton Dependencies...") |
| 93 | + cfg := &bootstrap.Config{ |
| 94 | + Namespace: "getting-started", |
| 95 | + Provider: "github", |
| 96 | + InstallDeps: true, |
| 97 | + CreateExamples: true, |
| 98 | + KubeConfig: config, |
| 99 | + |
| 100 | + GitHubRepo: "", |
| 101 | + GitHubToken: "", |
| 102 | + PublicDomain: "", |
| 103 | + WebhookSecret: "", |
| 104 | + } |
| 105 | + |
| 106 | + // Create bootstrapper |
| 107 | + bootstrapper, err := bootstrap.New(cfg) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("failed to create bootstrapper: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Run bootstrap (create namespace, RBAC, EventListener, Pipeline) |
| 113 | + if err := bootstrapper.Run(ctx); err != nil { |
| 114 | + return fmt.Errorf("bootstrap failed: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + log.Println("Tekton Dependencies are ready!") |
| 118 | + |
| 119 | + // run GitHub integration setup |
| 120 | + log.Println("\n== Configuring GitHub Integration ==") |
| 121 | + if err := runInteractiveSetup(); err != nil { |
| 122 | + log.Printf("⚠️ GitHub setup failed: %v\n", err) |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + // GitHub webhook creation if have the required info |
| 127 | + if githubRepo != "" && githubToken != "" && publicDomain != "" { |
| 128 | + log.Println("== Creating GitHub webhook ==") |
| 129 | + githubConfig := &bootstrap.Config{ |
| 130 | + Namespace: "getting-started", |
| 131 | + GitHubRepo: githubRepo, |
| 132 | + GitHubToken: githubToken, |
| 133 | + PublicDomain: publicDomain, |
| 134 | + WebhookSecret: webhookSecret, |
| 135 | + KubeConfig: config, |
| 136 | + } |
| 137 | + |
| 138 | + // Create GitHub manager and setup webhook |
| 139 | + githubManager := bootstrap.NewGitHubManager(githubConfig) |
| 140 | + if err := githubManager.SetupWebhook(ctx); err != nil { |
| 141 | + return fmt.Errorf("GitHub webhook setup failed: %w", err) |
| 142 | + } |
| 143 | + log.Println("GitHub webhook created!") |
| 144 | + } else { |
| 145 | + log.Println("⚠️ Missing required information") |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
0 commit comments