Skip to content
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

Add krel announce subcommand #1173

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ filegroup(
"//pkg/http:all-srcs",
"//pkg/kubepkg:all-srcs",
"//pkg/log:all-srcs",
"//pkg/mail:all-srcs",
"//pkg/notes:all-srcs",
"//pkg/patch:all-srcs",
"//pkg/release:all-srcs",
"//pkg/testgrid:all-srcs",
"//pkg/testing:all-srcs",
"//pkg/util:all-srcs",
"//pkg/version:all-srcs",
],
Expand Down
2 changes: 2 additions & 0 deletions cmd/krel/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"anago.go",
"announce.go",
"changelog.go",
"ff.go",
"gcbmgr.go",
Expand All @@ -24,6 +25,7 @@ go_library(
"//pkg/github:go_default_library",
"//pkg/http:go_default_library",
"//pkg/log:go_default_library",
"//pkg/mail:go_default_library",
"//pkg/notes:go_default_library",
"//pkg/notes/document:go_default_library",
"//pkg/notes/options:go_default_library",
Expand Down
185 changes: 185 additions & 0 deletions cmd/krel/cmd/announce.go
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"
saschagrunert marked this conversation as resolved.
Show resolved Hide resolved
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
}
43 changes: 43 additions & 0 deletions pkg/mail/BUILD.bazel
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"],
)
28 changes: 23 additions & 5 deletions pkg/patch/internal/mail_sender.go → pkg/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package internal
package mail

import (
"fmt"
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
Expand All @@ -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 {
Expand All @@ -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...)
}
Loading