Skip to content

Commit a54fa06

Browse files
Make change checks more fine-grained
1 parent 46e9cde commit a54fa06

File tree

6 files changed

+274
-15
lines changed

6 files changed

+274
-15
lines changed

cmd/kubeapply-lambda/main.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ var (
2222
sess *session.Session
2323
statsClient stats.StatsClient
2424

25-
automerge bool
26-
debug bool
27-
strictCheck bool
25+
automerge bool
26+
debug bool
27+
strictCheck bool
28+
greenCIRequired bool
29+
reviewRequired bool
2830

2931
logsURL = getLogsURL()
3032
)
@@ -75,6 +77,15 @@ var (
7577
// Optional, defaults to false.
7678
strictCheckStr = os.Getenv("KUBEAPPLY_STRICT_CHECK")
7779

80+
// Whether a green CI is required to apply. Ideally, should be set to "true",
81+
// but based on how long the CI takes, might be easier to have it be "false" in
82+
// non-production environments.
83+
greenCIRequiredStr = os.Getenv("KUBEAPPLY_GREEN_CI_REQUIRED")
84+
85+
// Whether a review is required to apply. Generally "true" in production and
86+
// otherwise "false".
87+
reviewRequiredStr = os.Getenv("KUBEAPPLY_REVIEW_REQUIRED")
88+
7889
// SSM parameter used for fetching webhook secret.
7990
webhookSecretSSMParam = os.Getenv("KUBEAPPLY_WEBHOOK_SECRET_SSM_PARAM")
8091
)
@@ -154,6 +165,14 @@ func init() {
154165
strictCheck = true
155166
}
156167

168+
if strings.ToLower(greenCIRequiredStr) == "true" {
169+
greenCIRequired = true
170+
}
171+
172+
if strings.ToLower(reviewRequiredStr) == "true" {
173+
reviewRequired = true
174+
}
175+
157176
if strings.ToLower(automergeStr) == "true" {
158177
automerge = true
159178
}
@@ -223,6 +242,8 @@ func handleRequest(
223242
Env: env,
224243
Version: version.Version,
225244
StrictCheck: strictCheck,
245+
GreenCIRequired: greenCIRequired,
246+
ReviewRequired: reviewRequired,
226247
Automerge: automerge,
227248
UseLocks: true,
228249
ApplyConsistencyCheck: false,

cmd/kubeapply-server/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ type Config struct {
4141
Env string `conf:"env" help:"only consider changes for this environment"`
4242
GithubToken string `conf:"github-token" help:"token for Github API access"`
4343
LogsURL string `conf:"logs-url" help:"url for logs; used as link for status checks"`
44-
StrictCheck bool `conf:"strict-check" help:"ensure green status and approval before apply"`
4544
WebhookSecret string `conf:"webhook-secret" help:"shared secret set in Github webhooks"`
45+
46+
// TODO: Deprecate StrictCheck since it's covered by the parameters below that.
47+
StrictCheck bool `conf:"strict-check" help:"ensure green status and approval before apply"`
48+
GreenCIRequired bool `conf:"green-ci-required" help:"require green CI before applying"`
49+
ReviewRequired bool `conf:"review-required" help:"require review before applying:"`
4650
}
4751

4852
var config = Config{
@@ -121,6 +125,8 @@ func webhookHTTPHandler(
121125
ApplyConsistencyCheck: false,
122126
Automerge: config.Automerge,
123127
StrictCheck: config.StrictCheck,
128+
GreenCIRequired: config.GreenCIRequired,
129+
ReviewRequired: config.ReviewRequired,
124130
Debug: config.Debug,
125131
},
126132
)

cmd/kubeapply/subcmd/pull_request.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,16 @@ type pullRequestFlags struct {
5656
// Full name of the repo, in [owner]/[name] format
5757
repo string
5858

59-
// Whether to be strict about checking for approvals and green github status
59+
// Whether to be strict about checking for approvals and green github status.
60+
//
61+
// Deprecated, to be replaced by the values below.
6062
strictCheck bool
63+
64+
// Whether green CI is required to apply
65+
greenCIRequired bool
66+
67+
// Whether a review is required to apply
68+
reviewRequired bool
6169
}
6270

6371
var pullRequestFlagValues pullRequestFlags
@@ -111,6 +119,12 @@ func init() {
111119
"",
112120
"Installation ID for github app",
113121
)
122+
pullRequestCmd.Flags().BoolVar(
123+
&pullRequestFlagValues.greenCIRequired,
124+
"green-ci-required",
125+
false,
126+
"Whether a green CI is required to apply",
127+
)
114128
pullRequestCmd.Flags().IntVar(
115129
&pullRequestFlagValues.pullRequestNum,
116130
"pull-request",
@@ -123,6 +137,12 @@ func init() {
123137
"",
124138
"Repo to post comment in, in format [owner]/[name]",
125139
)
140+
pullRequestCmd.Flags().BoolVar(
141+
&pullRequestFlagValues.reviewRequired,
142+
"review-required",
143+
false,
144+
"Whether a review is required to apply",
145+
)
126146
pullRequestCmd.Flags().BoolVar(
127147
&pullRequestFlagValues.strictCheck,
128148
"strict-check",
@@ -265,6 +285,8 @@ func pullRequestRun(cmd *cobra.Command, args []string) error {
265285
ApplyConsistencyCheck: false,
266286
Automerge: pullRequestFlagValues.automerge,
267287
StrictCheck: pullRequestFlagValues.strictCheck,
288+
GreenCIRequired: pullRequestFlagValues.greenCIRequired,
289+
ReviewRequired: pullRequestFlagValues.reviewRequired,
268290
Debug: debug,
269291
},
270292
)

pkg/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ type ClusterConfig struct {
7676
// Optional, defaults to false.
7777
GithubIgnore bool `json:"ignore"`
7878

79+
// ReviewOptional indicates that reviews should not be required for changes in this
80+
// cluster even if strict mode is on.
81+
//
82+
// Optional, and only applicable to webhooks mode.
83+
GithubReviewOptional bool `json:"reviewOptional"`
84+
7985
// VersionConstraint is a string version constraint against with the kubeapply binary
8086
// will be checked. See https://github.com/Masterminds/semver for details on the expected
8187
// format.

pkg/events/handler.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ type WebhookHandlerSettings struct {
5151

5252
// StrictCheck indicates whether we should block applies on having an approval and all
5353
// green statuses.
54+
//
55+
// Note: To be deprecated and replaced with CICheck and ReviewRequired below.
5456
StrictCheck bool
5557

58+
// GreenCIRequired indicates whether CI must be green before allowing applies.
59+
GreenCIRequired bool
60+
61+
// ReviewRequired indicates whether a review is required before allowing applies.
62+
ReviewRequired bool
63+
5664
// UseLocks indicates whether we should use locking to prevent overlapping handler calls
5765
// for a cluster.
5866
UseLocks bool
@@ -381,14 +389,29 @@ func (whh *WebhookHandler) runApply(
381389
Env: whh.settings.Env,
382390
}
383391

384-
if whh.settings.StrictCheck && !statusOK {
392+
overrideReviewRequired := true
393+
394+
for _, clusterClient := range clusterClients {
395+
if !clusterClient.Config().GithubReviewOptional {
396+
overrideReviewRequired = false
397+
break
398+
}
399+
}
400+
if overrideReviewRequired {
401+
log.Info(
402+
"Overriding review required because all clusters in this change have review optional",
403+
)
404+
}
405+
406+
if (whh.settings.StrictCheck || whh.settings.GreenCIRequired) && !statusOK {
385407
applyErr = multilineError(
386-
"Cannot run apply because strict-check is set to true and commit status is not green.",
408+
"Cannot run apply because green-ci-required is set to true and commit status is not green.",
387409
"Please fix status and try again.",
388410
)
389-
} else if whh.settings.StrictCheck && !approved {
411+
} else if (whh.settings.StrictCheck || whh.settings.ReviewRequired) &&
412+
!approved && !overrideReviewRequired {
390413
applyErr = multilineError(
391-
"Cannot run apply because strict-check is set to true and request is not approved.",
414+
"Cannot run apply because review-required is set to true and request is not approved.",
392415
"Please get at least one approval and try again.",
393416
)
394417
} else if behindBy > 0 {

0 commit comments

Comments
 (0)