Skip to content

Commit

Permalink
Refactor servicelog post such that it can be called from other commands
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVulaj committed Feb 17, 2023
1 parent 10c4ba9 commit a177d67
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 220 deletions.
13 changes: 11 additions & 2 deletions cmd/cluster/checkbanneduser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"fmt"
"github.com/openshift/osdctl/cmd/servicelog"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
Expand Down Expand Up @@ -63,8 +64,16 @@ func CheckBannedUser(clusterID string) error {
fmt.Println("User banned due to export control compliance.\nPlease follow the steps detailed here: https://github.com/openshift/ops-sop/blob/master/v4/alerts/UpgradeConfigSyncFailureOver4HrSRE.md#user-banneddisabled-due-to-export-control-compliance .")
return nil
}
fmt.Println("Recommend sending service log with:")
fmt.Printf("osdctl servicelog post -t https://raw.githubusercontent.com/openshift/managed-notifications/master/ocm/cluster_owner_disabled.json %v\n", clusterID)

fmt.Println("Sending service log.")
postCmd := servicelog.PostCmdOptions{
Template: "https://raw.githubusercontent.com/openshift/managed-notifications/master/ocm/cluster_owner_disabled.json",
ClusterId: clusterID,
}
if err = postCmd.Run(); err != nil {
return err
}

return nil
}
fmt.Println("User allowed")
Expand Down
42 changes: 34 additions & 8 deletions cmd/cluster/validatepullsecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
v1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/openshift/osdctl/cmd/servicelog"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -48,7 +49,7 @@ func ValidatePullSecret(clusterID string, kubeCli client.Client, flags *genericc
return err
}

clusterPullSecretEmail, err, done := getPullSecretEmail(clusterID, secret)
clusterPullSecretEmail, err, done := getPullSecretEmail(clusterID, secret, true)
if done {
return err
}
Expand All @@ -64,21 +65,37 @@ func ValidatePullSecret(clusterID string, kubeCli client.Client, flags *genericc
}

if account.Email() != clusterPullSecretEmail {
fmt.Println("Pull secret email doesn't match OCM user email. Recommend sending service log with:")
fmt.Printf("osdctl servicelog post -t https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/pull_secret_user_mismatch.json %v\n", clusterID)
fmt.Println("Pull secret email doesn't match OCM user email. Sending service log.")
postCmd := servicelog.PostCmdOptions{
Template: "https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/pull_secret_user_mismatch.json",
ClusterId: clusterID,
}
if err = postCmd.Run(); err != nil {
return err
}
return nil
}

fmt.Println("Email addresses match.")
return nil
}

func getPullSecretEmail(clusterID string, secret *corev1.Secret) (string, error, bool) {
func getPullSecretEmail(clusterID string, secret *corev1.Secret, sendServiceLog bool) (string, error, bool) {
dockerConfigJsonBytes, found := secret.Data[".dockerconfigjson"]
if !found {
// Indicates issue w/ pull-secret, so we can stop evaluating and specify a more direct course of action
fmt.Println("Secret does not contain expected key '.dockerconfigjson'. Recommend sending a service log with the following command:")
fmt.Printf("osdctl servicelog post -t https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/pull_secret_change_breaking_upgradesync.json %v\n", clusterID)
fmt.Println("Secret does not contain expected key '.dockerconfigjson'.")
if sendServiceLog {
fmt.Println("Sending service log.")
postCmd := servicelog.PostCmdOptions{
Template: "https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/pull_secret_change_breaking_upgradesync.json",
ClusterId: clusterID,
}
if err := postCmd.Run(); err != nil {
return "", err, true
}
}

return "", nil, true
}

Expand All @@ -89,8 +106,17 @@ func getPullSecretEmail(clusterID string, secret *corev1.Secret) (string, error,

cloudOpenshiftAuth, found := dockerConfigJson.Auths()["cloud.openshift.com"]
if !found {
fmt.Println("Secret does not contain entry for cloud.openshift.com. Recommend sending a service log with the following command:")
fmt.Printf("osdctl servicelog post -t https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/pull_secret_change_breaking_upgradesync.json %v\n", clusterID)
fmt.Println("Secret does not contain entry for cloud.openshift.com")
if sendServiceLog {
fmt.Println("Sending service log")
postCmd := servicelog.PostCmdOptions{
Template: "https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/pull_secret_change_breaking_upgradesync.json",
ClusterId: clusterID,
}
if err = postCmd.Run(); err != nil {
return "", err, true
}
}
return "", nil, true
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster/validatepullsecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_getPullSecretEmail(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
email, err, done := getPullSecretEmail("abc123", tt.secret)
email, err, done := getPullSecretEmail("abc123", tt.secret, false)
if email != tt.expectedEmail {
t.Errorf("getPullSecretEmail() email = %v, expectedEmail %v", email, tt.expectedEmail)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/servicelog/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func NewCmdServiceLog() *cobra.Command {
}

// Add subcommands
servicelogCmd.AddCommand(listCmd) // servicelog list
servicelogCmd.AddCommand(postCmd) // servicelog post
servicelogCmd.AddCommand(listCmd) // servicelog list
servicelogCmd.AddCommand(newPostCmd()) // servicelog post

return servicelogCmd
}
23 changes: 2 additions & 21 deletions cmd/servicelog/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var (
templateParams, userParameterNames, userParameterValues, filterParams []string
HTMLBody []byte
userParameterNames, userParameterValues, filterParams []string
HTMLBody []byte
)

const (
Expand All @@ -27,25 +27,6 @@ func sendRequest(request *sdk.Request) (*sdk.Response, error) {
return response, nil
}

func check(response *sdk.Response, clusterMessage servicelog.Message) {
body := response.Bytes()
if response.Status() < 400 {
_, err := validateGoodResponse(body, clusterMessage)
if err != nil {
failedClusters[clusterMessage.ClusterUUID] = err.Error()
} else {
successfulClusters[clusterMessage.ClusterUUID] = fmt.Sprintf("Message has been successfully sent to %s", clusterMessage.ClusterUUID)
}
} else {
badReply, err := validateBadResponse(body)
if err != nil {
failedClusters[clusterMessage.ClusterUUID] = err.Error()
} else {
failedClusters[clusterMessage.ClusterUUID] = badReply.Reason
}
}
}

func validateGoodResponse(body []byte, clusterMessage servicelog.Message) (goodReply *servicelog.GoodReply, err error) {
if !json.Valid(body) {
return nil, fmt.Errorf("server returned invalid JSON")
Expand Down
Loading

0 comments on commit a177d67

Please sign in to comment.