-
Notifications
You must be signed in to change notification settings - Fork 520
Add krel announce subcommand #1173
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
Merged
Merged
Changes from all commits
Commits
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
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
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,185 @@ | ||
/* | ||
Copyright 2020 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 cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"k8s.io/release/pkg/http" | ||
"k8s.io/release/pkg/mail" | ||
"k8s.io/release/pkg/release" | ||
"k8s.io/release/pkg/util" | ||
) | ||
|
||
const ( | ||
sendgridAPIKeyFlag = "sendgrid-api-key" | ||
sendgridAPIKeyEnvKey = "SENDGRID_API_KEY" | ||
nameFlag = "name" | ||
emailFlag = "email" | ||
tagFlag = "tag" | ||
) | ||
|
||
// announceCmd represents the subcommand for `krel announce` | ||
var announceCmd = &cobra.Command{ | ||
Use: "announce", | ||
Short: "Announce Kubernetes releases", | ||
Long: fmt.Sprintf(`krel announce | ||
|
||
krel announce can be used to announce already built Kubernetes releases to the | ||
%q and %q Google Group. | ||
|
||
If --nomock=true (the default), then the mail will be sent only to a test | ||
Google Group %q. | ||
|
||
It is necessary to either set a valid --%s,-s or export the | ||
$%s environment variable. An API key can be created by | ||
registering a sendgrid.com account and adding the key here: | ||
|
||
https://app.sendgrid.com/settings/api_keys | ||
|
||
Beside this, the flags for a valid sender name (--%s,-n), sender email | ||
address (--%s,-e) and a valid Kubernetes tag (--%s,-t) have to be set | ||
as well.`, | ||
mail.KubernetesAnnounceGoogleGroup, | ||
mail.KubernetesDevGoogleGroup, | ||
mail.KubernetesAnnounceTestGoogleGroup, | ||
sendgridAPIKeyFlag, | ||
sendgridAPIKeyEnvKey, | ||
nameFlag, | ||
emailFlag, | ||
tagFlag, | ||
), | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runAnnounce(announceOpts, rootOpts) | ||
}, | ||
} | ||
|
||
type announceOptions struct { | ||
sendgridAPIKey string | ||
name string | ||
email string | ||
tag string | ||
} | ||
|
||
var announceOpts = &announceOptions{} | ||
|
||
func init() { | ||
announceCmd.PersistentFlags().StringVarP( | ||
&announceOpts.sendgridAPIKey, | ||
sendgridAPIKeyFlag, | ||
"s", | ||
util.EnvDefault(sendgridAPIKeyEnvKey, ""), | ||
fmt.Sprintf( | ||
"API key for sendgrid, can be set via %s too", | ||
sendgridAPIKeyEnvKey, | ||
), | ||
) | ||
|
||
announceCmd.PersistentFlags().StringVarP( | ||
&announceOpts.name, | ||
nameFlag, | ||
"n", | ||
"", | ||
"mail sender name", | ||
) | ||
if err := announceCmd.MarkPersistentFlagRequired(nameFlag); err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
announceCmd.PersistentFlags().StringVarP( | ||
&announceOpts.email, | ||
emailFlag, | ||
"e", | ||
"", | ||
"email address", | ||
) | ||
if err := announceCmd.MarkPersistentFlagRequired(emailFlag); err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
announceCmd.PersistentFlags().StringVarP( | ||
&announceOpts.tag, | ||
tagFlag, | ||
"t", | ||
"", | ||
"built tag to be announced, will be used for fetching the "+ | ||
"announcement from the google cloud bucket", | ||
) | ||
if err := announceCmd.MarkPersistentFlagRequired(tagFlag); err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
rootCmd.AddCommand(announceCmd) | ||
} | ||
|
||
func runAnnounce(opts *announceOptions, rootOpts *rootOptions) error { | ||
if opts.sendgridAPIKey == "" { | ||
return errors.Errorf( | ||
"Neither --sendgrid-api-key,-s nor $%s is set", sendgridAPIKeyEnvKey, | ||
) | ||
} | ||
|
||
logrus.Info("Retrieving release announcement from Google Cloud Bucket") | ||
|
||
tag := util.AddTagPrefix(opts.tag) | ||
u := fmt.Sprintf( | ||
"%s/archive/anago-%s/announcement.html", | ||
release.URLPrefixForBucket(release.ProductionBucket), tag, | ||
) | ||
logrus.Infof("Using announcement remote URL: %s", u) | ||
|
||
content, err := http.GetURLResponse(u, false) | ||
if err != nil { | ||
return errors.Wrapf(err, | ||
"unable to retrieve release announcement form url: %s", u, | ||
) | ||
} | ||
|
||
logrus.Info("Preparing mail sender") | ||
m := mail.Sender{APIKey: opts.sendgridAPIKey} | ||
|
||
if err := m.SetSender(opts.name, opts.email); err != nil { | ||
return errors.Wrap(err, "unable to set mail sender") | ||
} | ||
|
||
groups := []mail.GoogleGroup{mail.KubernetesAnnounceTestGoogleGroup} | ||
if rootOpts.nomock { | ||
groups = []mail.GoogleGroup{ | ||
mail.KubernetesAnnounceGoogleGroup, | ||
mail.KubernetesDevGoogleGroup, | ||
} | ||
} | ||
logrus.Infof("Using Google Groups as announcement target: %v", groups) | ||
|
||
if err := m.SetGoogleGroupRecipients(groups...); err != nil { | ||
return errors.Wrap(err, "unable to set mail recipients") | ||
} | ||
|
||
logrus.Info("Sending mail") | ||
subject := fmt.Sprintf("Kubernetes %s is live!", tag) | ||
if err := m.Send(content, subject); err != nil { | ||
return errors.Wrap(err, "unable to send mail") | ||
} | ||
|
||
return nil | ||
} |
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,43 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["mail.go"], | ||
importpath = "k8s.io/release/pkg/mail", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/log:go_default_library", | ||
"@com_github_sendgrid_rest//:go_default_library", | ||
"@com_github_sendgrid_sendgrid_go//:go_default_library", | ||
"@com_github_sendgrid_sendgrid_go//helpers/mail:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["mail_test.go"], | ||
embed = [":go_default_library"], | ||
deps = [ | ||
"//pkg/mail/mailfakes:go_default_library", | ||
"//pkg/testing:go_default_library", | ||
"@com_github_sendgrid_rest//:go_default_library", | ||
"@com_github_stretchr_testify//require:go_default_library", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "package-srcs", | ||
srcs = glob(["**"]), | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
filegroup( | ||
name = "all-srcs", | ||
srcs = [ | ||
":package-srcs", | ||
"//pkg/mail/mailfakes:all-srcs", | ||
], | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:public"], | ||
) |
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 |
---|---|---|
|
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and | |
limitations under the License. | ||
*/ | ||
|
||
package internal | ||
package mail | ||
|
||
import ( | ||
"fmt" | ||
|
@@ -25,7 +25,16 @@ import ( | |
"k8s.io/release/pkg/log" | ||
) | ||
|
||
type MailSender struct { | ||
// GoogleGroup is a simple google group representation | ||
type GoogleGroup string | ||
|
||
const ( | ||
KubernetesAnnounceGoogleGroup GoogleGroup = "kubernetes-announce" | ||
saschagrunert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
KubernetesDevGoogleGroup GoogleGroup = "kubernetes-dev" | ||
KubernetesAnnounceTestGoogleGroup GoogleGroup = "kubernetes-announce-test" | ||
) | ||
|
||
type Sender struct { | ||
log.Mixin | ||
|
||
SendgridClientCreator SendgridClientCreator | ||
|
@@ -55,7 +64,7 @@ var defaultSendgridClientCreator = func(apiKey string) SendgridClient { | |
return sendgrid.NewSendClient(apiKey) | ||
} | ||
|
||
func (m *MailSender) Send(body, subject string) error { | ||
func (m *Sender) Send(body, subject string) error { | ||
html := mail.NewContent("text/html", body) | ||
|
||
p := mail.NewPersonalization() | ||
|
@@ -95,7 +104,7 @@ func (e *SendError) Error() string { | |
return fmt.Sprintf("got code %d while sending: Body: %q, Header: %q", e.code, e.resBody, e.resHeaders) | ||
} | ||
|
||
func (m *MailSender) SetSender(name, email string) error { | ||
func (m *Sender) SetSender(name, email string) error { | ||
if email == "" { | ||
return fmt.Errorf("email must not be empty") | ||
} | ||
|
@@ -104,7 +113,7 @@ func (m *MailSender) SetSender(name, email string) error { | |
return nil | ||
} | ||
|
||
func (m *MailSender) SetRecipients(recipientArgs ...string) error { | ||
func (m *Sender) SetRecipients(recipientArgs ...string) error { | ||
l := len(recipientArgs) | ||
|
||
if l%2 != 0 { | ||
|
@@ -127,3 +136,12 @@ func (m *MailSender) SetRecipients(recipientArgs ...string) error { | |
|
||
return nil | ||
} | ||
|
||
// SetGoogleGroupRecipient can be used to set multiple Google Groups as recipient | ||
func (m *Sender) SetGoogleGroupRecipients(groups ...GoogleGroup) error { | ||
args := []string{} | ||
for _, group := range groups { | ||
args = append(args, string(group), fmt.Sprintf("%[email protected]", group)) | ||
} | ||
return m.SetRecipients(args...) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.