-
Notifications
You must be signed in to change notification settings - Fork 218
external-plugins: add netlify-preview plugin to retry deploy previews #708
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
Open
Caesarsage
wants to merge
6
commits into
kubernetes-sigs:main
Choose a base branch
from
Caesarsage:netlify-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
08f2e35
external-plugins/netlify-preview: add Netlify API client
Caesarsage e6ba2ea
external-plugins/netlify-preview: add per-repo site mapping config
Caesarsage 726f61c
external-plugins/netlify-preview: add command parser and decision logic
Caesarsage a8b242c
external-plugins/netlify-preview: wire HTTP server and command handler
Caesarsage 0d14214
external-plugins/netlify-preview: register image build and add plugin…
Caesarsage 35fd839
external-plugins/netlify-preview: drop year from copyright header
Caesarsage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,64 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| type Config struct { | ||
| Repos map[string]Repo `json:"repos,omitempty"` | ||
| } | ||
|
|
||
| type Repo struct { | ||
| SiteID string `json:"site_id,omitempty"` | ||
| } | ||
|
|
||
| func Load(path string) (*Config, error) { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var cfg Config | ||
| if err := yaml.UnmarshalStrict(data, &cfg); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := cfg.Validate(); err != nil { | ||
| return nil, err | ||
| } | ||
| return &cfg, nil | ||
| } | ||
|
|
||
| func (c *Config) Validate() error { | ||
| for repo, cfg := range c.Repos { | ||
| if cfg.SiteID == "" { | ||
| return fmt.Errorf("missing site_id for repo %q", repo) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *Config) Repo(org, repo string) (Repo, bool) { | ||
| if c == nil { | ||
| return Repo{}, false | ||
| } | ||
| cfg, ok := c.Repos[org+"/"+repo] | ||
| return cfg, ok | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
cmd/external-plugins/netlify-preview/config/config_test.go
This file contains hidden or 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,61 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLoad(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "config.yaml") | ||
| if err := os.WriteFile(path, []byte(`repos: | ||
| kubernetes/website: | ||
| site_id: site-123 | ||
| `), 0600); err != nil { | ||
| t.Fatalf("failed to write config: %v", err) | ||
| } | ||
|
|
||
| cfg, err := Load(path) | ||
| if err != nil { | ||
| t.Fatalf("failed to load config: %v", err) | ||
| } | ||
|
|
||
| repo, ok := cfg.Repo("kubernetes", "website") | ||
| if !ok { | ||
| t.Fatal("expected kubernetes/website mapping") | ||
| } | ||
| if repo.SiteID != "site-123" { | ||
| t.Fatalf("expected site id %q, got %q", "site-123", repo.SiteID) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadRejectsMissingSiteID(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "config.yaml") | ||
| if err := os.WriteFile(path, []byte(`repos: | ||
| kubernetes/website: {} | ||
| `), 0600); err != nil { | ||
| t.Fatalf("failed to write config: %v", err) | ||
| } | ||
|
|
||
| if _, err := Load(path); err == nil { | ||
| t.Fatal("expected error for missing site id") | ||
| } | ||
| } |
This file contains hidden or 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,147 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| // netlify-preview retries Netlify deploy previews for pull requests. | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| previewconfig "sigs.k8s.io/prow/cmd/external-plugins/netlify-preview/config" | ||
| "sigs.k8s.io/prow/cmd/external-plugins/netlify-preview/netlify" | ||
| netlifypreview "sigs.k8s.io/prow/cmd/external-plugins/netlify-preview/plugin" | ||
| "sigs.k8s.io/prow/pkg/config/secret" | ||
| "sigs.k8s.io/prow/pkg/flagutil" | ||
| prowflagutil "sigs.k8s.io/prow/pkg/flagutil" | ||
| pluginsflagutil "sigs.k8s.io/prow/pkg/flagutil/plugins" | ||
| "sigs.k8s.io/prow/pkg/interrupts" | ||
| "sigs.k8s.io/prow/pkg/logrusutil" | ||
| "sigs.k8s.io/prow/pkg/pjutil" | ||
| "sigs.k8s.io/prow/pkg/pluginhelp/externalplugins" | ||
| ) | ||
|
|
||
| type options struct { | ||
| port int | ||
|
|
||
| pluginsConfig pluginsflagutil.PluginOptions | ||
| dryRun bool | ||
| github prowflagutil.GitHubOptions | ||
| instrumentationOptions prowflagutil.InstrumentationOptions | ||
| logLevel string | ||
|
|
||
| webhookSecretFile string | ||
| netlifyTokenFile string | ||
| configPath string | ||
| netlifyAPIURL string | ||
| } | ||
|
|
||
| func (o *options) Validate() error { | ||
| for idx, group := range []flagutil.OptionGroup{&o.github} { | ||
| if err := group.Validate(o.dryRun); err != nil { | ||
| return fmt.Errorf("%d: %w", idx, err) | ||
| } | ||
| } | ||
| if o.netlifyTokenFile == "" { | ||
| return fmt.Errorf("--netlify-token-file is required") | ||
| } | ||
| if o.configPath == "" { | ||
| return fmt.Errorf("--config-path is required") | ||
| } | ||
| if o.netlifyAPIURL == "" { | ||
| return fmt.Errorf("--netlify-api-url is required") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func gatherOptions() options { | ||
| o := options{} | ||
| fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
| fs.IntVar(&o.port, "port", 8888, "Port to listen on.") | ||
| fs.BoolVar(&o.dryRun, "dry-run", true, "Dry run for testing. Uses API tokens but does not mutate.") | ||
| fs.StringVar(&o.webhookSecretFile, "hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.") | ||
| fs.StringVar(&o.netlifyTokenFile, "netlify-token-file", "/etc/netlify-preview/token", "Path to the file containing the Netlify API token.") | ||
| fs.StringVar(&o.configPath, "config-path", "/etc/netlify-preview/config.yaml", "Path to the netlify-preview plugin config file.") | ||
| fs.StringVar(&o.netlifyAPIURL, "netlify-api-url", "https://api.netlify.com", "Base URL for the Netlify API.") | ||
| fs.StringVar(&o.logLevel, "log-level", "debug", fmt.Sprintf("Log level is one of %v.", logrus.AllLevels)) | ||
| o.pluginsConfig.PluginConfigPathDefault = "/etc/plugins/plugins.yaml" | ||
| for _, group := range []flagutil.OptionGroup{&o.github, &o.instrumentationOptions, &o.pluginsConfig} { | ||
| group.AddFlags(fs) | ||
| } | ||
| fs.Parse(os.Args[1:]) | ||
| return o | ||
| } | ||
|
|
||
| func main() { | ||
| logrusutil.ComponentInit() | ||
| o := gatherOptions() | ||
| if err := o.Validate(); err != nil { | ||
| logrus.Fatalf("Invalid options: %v", err) | ||
| } | ||
|
|
||
| logLevel, err := logrus.ParseLevel(o.logLevel) | ||
| if err != nil { | ||
| logrus.WithError(err).Fatal("Failed to parse loglevel") | ||
| } | ||
| logrus.SetLevel(logLevel) | ||
| log := logrus.StandardLogger().WithField("plugin", netlifypreview.PluginName) | ||
|
|
||
| if err := secret.Add(o.webhookSecretFile); err != nil { | ||
| logrus.WithError(err).Fatal("Error starting webhook secret agent.") | ||
| } | ||
| if err := secret.Add(o.netlifyTokenFile); err != nil { | ||
| logrus.WithError(err).Fatal("Error starting Netlify token secret agent.") | ||
| } | ||
|
|
||
| pluginAgent, err := o.pluginsConfig.PluginAgent() | ||
| if err != nil { | ||
| log.WithError(err).Fatal("Error loading plugin config.") | ||
| } | ||
| previewConfig, err := previewconfig.Load(o.configPath) | ||
| if err != nil { | ||
| log.WithError(err).Fatal("Error loading netlify-preview config.") | ||
| } | ||
| githubClient, err := o.github.GitHubClient(o.dryRun) | ||
| if err != nil { | ||
| logrus.WithError(err).Fatal("Error getting GitHub client.") | ||
| } | ||
|
|
||
| serv := &server{ | ||
| tokenGenerator: secret.GetTokenGenerator(o.webhookSecretFile), | ||
| ghc: githubClient, | ||
| netlifyClient: netlify.NewClient(o.netlifyAPIURL, http.DefaultClient, secret.GetTokenGenerator(o.netlifyTokenFile)), | ||
| pluginConfig: pluginAgent, | ||
| previewConfig: previewConfig, | ||
| log: log, | ||
| dryRun: o.dryRun, | ||
| } | ||
|
|
||
| health := pjutil.NewHealthOnPort(o.instrumentationOptions.HealthPort) | ||
| health.ServeReady() | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.Handle("/", serv) | ||
| externalplugins.ServeExternalPluginHelp(mux, log, netlifypreview.HelpProvider) | ||
| httpServer := &http.Server{Addr: ":" + strconv.Itoa(o.port), Handler: mux} | ||
| defer interrupts.WaitForGracefulShutdown() | ||
| interrupts.ListenAndServe(httpServer, 5*time.Second) | ||
| } |
This file contains hidden or 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,107 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package netlify | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // Deploy is the subset of Netlify deploy data needed to retry PR deploy previews. | ||
| type Deploy struct { | ||
| ID string `json:"id"` | ||
| Context string `json:"context"` | ||
| State string `json:"state"` | ||
| ReviewID int `json:"review_id"` | ||
| Branch string `json:"branch"` | ||
| DeploySSLURL string `json:"deploy_ssl_url"` | ||
| CreatedAt time.Time `json:"created_at"` | ||
| } | ||
|
|
||
| type Client struct { | ||
| baseURL string | ||
| httpClient *http.Client | ||
| tokenGenerator func() []byte | ||
| } | ||
|
|
||
| func NewClient(baseURL string, httpClient *http.Client, tokenGenerator func() []byte) *Client { | ||
| if httpClient == nil { | ||
| httpClient = http.DefaultClient | ||
| } | ||
| return &Client{ | ||
| baseURL: strings.TrimRight(baseURL, "/"), | ||
| httpClient: httpClient, | ||
| tokenGenerator: tokenGenerator, | ||
| } | ||
| } | ||
|
|
||
| func (c *Client) ListDeploys(ctx context.Context, siteID string) ([]Deploy, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/api/v1/sites/%s/deploys", c.baseURL, url.PathEscape(siteID)), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| c.authorize(req) | ||
| resp, err := c.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode < 200 || resp.StatusCode > 299 { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| return nil, fmt.Errorf("list deploys returned %s: %s", resp.Status, strings.TrimSpace(string(body))) | ||
| } | ||
| var deploys []Deploy | ||
| if err := json.NewDecoder(resp.Body).Decode(&deploys); err != nil { | ||
| return nil, err | ||
| } | ||
| return deploys, nil | ||
| } | ||
|
|
||
| func (c *Client) RetryDeploy(ctx context.Context, deployID string) error { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/api/v1/deploys/%s/retry", c.baseURL, url.PathEscape(deployID)), nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.authorize(req) | ||
| resp, err := c.httpClient.Do(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode < 200 || resp.StatusCode > 299 { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| return fmt.Errorf("retry deploy returned %s: %s", resp.Status, strings.TrimSpace(string(body))) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *Client) authorize(req *http.Request) { | ||
| if c.tokenGenerator == nil { | ||
| return | ||
| } | ||
| token := strings.TrimSpace(string(c.tokenGenerator())) | ||
| if token == "" { | ||
| return | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: type name
Config&Repois a bit misleading wrapper.maybe:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Config is the configuration wrapper to the repository and each repository mapped to their netlify site id.
such as: