-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We've evolved a lot over time, and we could start nudging developers to use the now-recommended approaches and avoid the less-recommended approaches (without breaking compatability). Introduce a "hints" package that enables this.
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,33 @@ | ||
package hints | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
// EnvRecommendationsAreStrict is the environment variable that can be set | ||
// to make developer recommendations a panic. It can be set by developers | ||
// while developing to help them keep up with changes to | ||
// kubebuilder-declarative-pattern and any upstream changes. | ||
const EnvRecommendationsAreStrict = "FAIL_ON_DEVELOPER_RECOMMENDATIONS" | ||
|
||
// DeveloperRecommendation will log a recommendation for developers; | ||
// helping developers keep up with changes here and upstream. | ||
// If EnvRecommendationsAreStrict is set, we will panic. | ||
func DeveloperRecommendation(ctx context.Context, msg string, keysAndValues ...any) { | ||
log := klog.FromContext(ctx) | ||
log.Info(msg, keysAndValues...) | ||
if os.Getenv(EnvRecommendationsAreStrict) != "" { | ||
s := fmt.Sprintf("recommendation for developers with %s environment variable set, so treating as a failure: %s", EnvRecommendationsAreStrict, msg) | ||
for i := 0; (i + 1) < len(keysAndValues); i++ { | ||
if i != 0 { | ||
s += ", " | ||
} | ||
s += fmt.Sprintf("%v:%v", keysAndValues[i], keysAndValues[i+1]) | ||
} | ||
panic(s) | ||
} | ||
} |
This file contains 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 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