@@ -17,27 +17,23 @@ limitations under the License.
1717package cmd
1818
1919import (
20+ "context"
2021 "errors"
2122 "fmt"
2223
2324 "github.com/sirupsen/logrus"
2425 "github.com/spf13/cobra"
2526
26- "sigs.k8s.io/release-utils/env"
2727 "sigs.k8s.io/release-utils/helpers"
2828 "sigs.k8s.io/release-utils/http"
2929
3030 "k8s.io/release/pkg/mail"
3131 "k8s.io/release/pkg/release"
3232)
3333
34- const (
35- sendgridAPIKeyEnvKey = "SENDGRID_API_KEY" //nolint:gosec // it's just the key
36- nameFlag = "name"
37- emailFlag = "email"
38- )
34+ const noBrowserFlag = "no-browser"
3935
40- // announceCmd represents the subcommand for `krel announce`.
36+ // sendAnnounceCmd represents the subcommand for `krel announce send `.
4137var sendAnnounceCmd = & cobra.Command {
4238 Use : "send" ,
4339 Short : "Announce Kubernetes releases" ,
@@ -47,28 +43,26 @@ krel announce send can be used to mail an announcement of an already
4743built Kubernetes release to the %q and %q Google Groups.
4844
4945By default the mail will be sent only to a test Google Group %q,
50- ie: the announcement run will only be a mock run. To do an
46+ ie: the announcement run will only be a mock run. To do an
5147official announcement, use the --nomock flag.
5248
53- It is necessary to export the $%s environment variable. An API key can be created by
54- registering a sendgrid.com account and adding the key here:
55-
56- https://app.sendgrid.com/settings/api_keys
49+ Email is sent via the Gmail API using Google OAuth. A browser window
50+ will open for authorization on each run. No additional setup is
51+ needed.
5752
58- Beside this, if the flags for a valid sender name (--%s,-n) and sender email
59- address (--%s,-e) are not set, then it tries to retrieve those values directly
60- from the Sendgrid API.
53+ Use --%s to print the authorization URL for manual copy/paste
54+ (useful in headless environments). After authorizing in the browser,
55+ the redirect will fail to load. Copy the full URL from the browser's
56+ address bar and paste it back into the terminal.
6157
6258Setting a valid Kubernetes tag (--%s,-t) is always necessary.
6359
64- If --%s,-p is given, then krel announce will only print the email content
65- without doing anything else.` ,
60+ If --%s,-p is given, then krel announce will only print the email
61+ content without doing anything else.` ,
6662 mail .KubernetesAnnounceGoogleGroup ,
6763 mail .KubernetesDevGoogleGroup ,
6864 mail .KubernetesAnnounceTestGoogleGroup ,
69- sendgridAPIKeyEnvKey ,
70- nameFlag ,
71- emailFlag ,
65+ noBrowserFlag ,
7266 tagFlag ,
7367 printOnlyFlag ,
7468 ),
@@ -80,30 +74,17 @@ without doing anything else.`,
8074}
8175
8276type sendAnnounceOptions struct {
83- sendgridAPIKey string
84- name string
85- email string
77+ noBrowser bool
8678}
8779
8880var sendAnnounceOpts = & sendAnnounceOptions {}
8981
9082func init () {
91- sendAnnounceOpts .sendgridAPIKey = env .Default (sendgridAPIKeyEnvKey , "" )
92-
93- sendAnnounceCmd .PersistentFlags ().StringVarP (
94- & sendAnnounceOpts .name ,
95- nameFlag ,
96- "n" ,
97- "" ,
98- "mail sender name" ,
99- )
100-
101- sendAnnounceCmd .PersistentFlags ().StringVarP (
102- & sendAnnounceOpts .email ,
103- emailFlag ,
104- "e" ,
105- "" ,
106- "email address" ,
83+ sendAnnounceCmd .PersistentFlags ().BoolVar (
84+ & sendAnnounceOpts .noBrowser ,
85+ noBrowserFlag ,
86+ false ,
87+ "disable automatic browser opening for OAuth (manual URL copy/paste)" ,
10788 )
10889
10990 announceCmd .AddCommand (sendAnnounceCmd )
@@ -126,7 +107,7 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r
126107 content , err := http .NewAgent ().Get (u )
127108 if err != nil {
128109 return fmt .Errorf (
129- "unable to retrieve release announcement form url: %s: %w" , u , err ,
110+ "unable to retrieve release announcement from url: %s: %w" , u , err ,
130111 )
131112 }
132113
@@ -137,26 +118,11 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r
137118 return nil
138119 }
139120
140- if opts .sendgridAPIKey == "" {
141- return fmt .Errorf (
142- "$%s is not set" , sendgridAPIKeyEnvKey ,
143- )
144- }
145-
146- logrus .Info ("Preparing mail sender" )
147-
148- m := mail .NewSender (opts .sendgridAPIKey )
149-
150- if opts .name != "" && opts .email != "" {
151- if err := m .SetSender (opts .name , opts .email ); err != nil {
152- return fmt .Errorf ("unable to set mail sender: %w" , err )
153- }
154- } else {
155- logrus .Info ("Retrieving default sender from sendgrid API" )
121+ logrus .Info ("Starting Gmail OAuth flow" )
156122
157- if err := m . SetDefaultSender (); err != nil {
158- return fmt . Errorf ( "setting default sender: %w" , err )
159- }
123+ sender , err := mail . NewGmailSender ( context . Background (), opts . noBrowser )
124+ if err != nil {
125+ return fmt . Errorf ( "creating Gmail sender: %w" , err )
160126 }
161127
162128 groups := []mail.GoogleGroup {mail .KubernetesAnnounceTestGoogleGroup }
@@ -169,9 +135,7 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r
169135
170136 logrus .Infof ("Using Google Groups as announcement target: %v" , groups )
171137
172- if err := m .SetGoogleGroupRecipients (groups ... ); err != nil {
173- return fmt .Errorf ("unable to set mail recipients: %w" , err )
174- }
138+ sender .SetGoogleGroupRecipients (groups ... )
175139
176140 logrus .Info ("Sending mail" )
177141
@@ -187,7 +151,7 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r
187151 }
188152
189153 if yes {
190- if err := m .Send (string (content ), subject ); err != nil {
154+ if err := sender .Send (string (content ), subject ); err != nil {
191155 return fmt .Errorf ("unable to send mail: %w" , err )
192156 }
193157 }
0 commit comments