Skip to content

Commit d12d5f1

Browse files
Merge pull request #2775 from 2uasimojo/le-big-refactor
Refactor some things
2 parents 161a023 + 9ccaa4c commit d12d5f1

File tree

130 files changed

+1058
-1300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1058
-1300
lines changed

cmd/manager/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"flag"
66
golog "log"
7-
"math/rand"
87
"net/http"
98
_ "net/http/pprof"
109
"os"
@@ -258,7 +257,6 @@ func main() {
258257
log.WithField("pprof_host_port", pprofHostPort).Info("Enabling pprof")
259258
log.Println(http.ListenAndServe(pprofHostPort, nil))
260259
}()
261-
rand.Seed(time.Now().UnixNano())
262260
cmd := newRootCommand()
263261
err := cmd.Execute()
264262
if err != nil {

contrib/pkg/adm/managedns/enable.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/client/config"
2828

2929
hivev1 "github.com/openshift/hive/apis/hive/v1"
30-
hiveutils "github.com/openshift/hive/contrib/pkg/utils"
31-
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
32-
azureutils "github.com/openshift/hive/contrib/pkg/utils/azure"
33-
gcputils "github.com/openshift/hive/contrib/pkg/utils/gcp"
30+
"github.com/openshift/hive/contrib/pkg/utils"
3431
"github.com/openshift/hive/pkg/constants"
32+
awscreds "github.com/openshift/hive/pkg/creds/aws"
33+
azurecreds "github.com/openshift/hive/pkg/creds/azure"
34+
gcpcreds "github.com/openshift/hive/pkg/creds/gcp"
3535
"github.com/openshift/hive/pkg/resource"
3636
"github.com/openshift/hive/pkg/util/scheme"
3737
)
@@ -46,9 +46,6 @@ managed domains, create a credentials secret for your cloud provider, and link i
4646
the ExternalDNS section of HiveConfig.
4747
`
4848
const (
49-
cloudAWS = "aws"
50-
cloudGCP = "gcp"
51-
cloudAzure = "azure"
5249
hiveAdmissionDeployment = "hiveadmission"
5350
hiveConfigName = "hive"
5451
waitTime = time.Minute * 2
@@ -95,7 +92,7 @@ func NewEnableManageDNSCommand() *cobra.Command {
9592
}
9693

9794
flags := cmd.Flags()
98-
flags.StringVar(&opt.Cloud, "cloud", cloudAWS, "Cloud provider: aws(default)|gcp|azure)")
95+
flags.StringVar(&opt.Cloud, "cloud", constants.PlatformAWS, "Cloud provider: aws(default)|gcp|azure)")
9996
flags.StringVar(&opt.CredsFile, "creds-file", "", "Cloud credentials file (defaults vary depending on cloud)")
10097
flags.StringVar(&opt.AzureResourceGroup, "azure-resource-group-name", "os4-common", "Azure Resource Group (Only applicable if --cloud azure)")
10198
return cmd
@@ -127,8 +124,7 @@ func (o *Options) Run(args []string) error {
127124
// Update the current HiveConfig, which should always exist as the operator will
128125
// create a default one once run.
129126
hc := &hivev1.HiveConfig{}
130-
o.hiveClient.Get(context.TODO(), types.NamespacedName{Name: hiveConfigName}, hc)
131-
if err != nil {
127+
if err := o.hiveClient.Get(context.TODO(), types.NamespacedName{Name: hiveConfigName}, hc); err != nil {
132128
log.WithError(err).Fatal("error looking up HiveConfig 'hive'")
133129
}
134130

@@ -139,7 +135,7 @@ func (o *Options) Run(args []string) error {
139135
var credsSecret *corev1.Secret
140136

141137
switch o.Cloud {
142-
case cloudAWS:
138+
case constants.PlatformAWS:
143139
// Apply a secret for credentials to manage the root domain:
144140
credsSecret, err = o.generateAWSCredentialsSecret()
145141
if err != nil {
@@ -148,7 +144,7 @@ func (o *Options) Run(args []string) error {
148144
dnsConf.AWS = &hivev1.ManageDNSAWSConfig{
149145
CredentialsSecretRef: corev1.LocalObjectReference{Name: credsSecret.Name},
150146
}
151-
case cloudGCP:
147+
case constants.PlatformGCP:
152148
// Apply a secret for credentials to manage the root domain:
153149
credsSecret, err = o.generateGCPCredentialsSecret()
154150
if err != nil {
@@ -157,7 +153,7 @@ func (o *Options) Run(args []string) error {
157153
dnsConf.GCP = &hivev1.ManageDNSGCPConfig{
158154
CredentialsSecretRef: corev1.LocalObjectReference{Name: credsSecret.Name},
159155
}
160-
case cloudAzure:
156+
case constants.PlatformAzure:
161157
credsSecret, err = o.generateAzureCredentialsSecret()
162158
if err != nil {
163159
log.WithError(err).Fatal("error generating manageDNS credentials secret")
@@ -313,7 +309,7 @@ func (o *Options) waitForHiveConfigToBeProcessed() error {
313309

314310
func (o *Options) generateAWSCredentialsSecret() (*corev1.Secret, error) {
315311
defaultCredsFilePath := filepath.Join(o.homeDir, ".aws", "credentials")
316-
accessKeyID, secretAccessKey, err := awsutils.GetAWSCreds(o.CredsFile, defaultCredsFilePath)
312+
accessKeyID, secretAccessKey, err := awscreds.GetAWSCreds(o.CredsFile, defaultCredsFilePath)
317313
if err != nil {
318314
return nil, err
319315
}
@@ -334,7 +330,7 @@ func (o *Options) generateAWSCredentialsSecret() (*corev1.Secret, error) {
334330
}
335331

336332
func (o *Options) generateGCPCredentialsSecret() (*corev1.Secret, error) {
337-
saFileContents, err := gcputils.GetCreds(o.CredsFile)
333+
saFileContents, err := gcpcreds.GetCreds(o.CredsFile)
338334
if err != nil {
339335
return nil, err
340336
}
@@ -354,7 +350,7 @@ func (o *Options) generateGCPCredentialsSecret() (*corev1.Secret, error) {
354350
}
355351

356352
func (o *Options) generateAzureCredentialsSecret() (*corev1.Secret, error) {
357-
spFileContents, err := azureutils.GetCreds(o.CredsFile)
353+
spFileContents, err := azurecreds.GetCreds(o.CredsFile)
358354
if err != nil {
359355
return nil, err
360356
}
@@ -379,12 +375,15 @@ func (o *Options) getResourceHelper() (resource.Helper, error) {
379375
log.WithError(err).Error("Cannot get client config")
380376
return nil, err
381377
}
382-
return resource.NewHelperFromRESTConfig(cfg, "util-managedns-enable", log.WithField("command", "adm manage-dns enable"))
378+
return resource.NewHelper(
379+
log.WithField("command", "adm manage-dns enable"),
380+
resource.FromRESTConfig(cfg),
381+
resource.WithControllerName("util-managedns-enable"))
383382
}
384383

385384
func (o *Options) setupLocalClients() error {
386385
log.Debug("creating cluster client config")
387-
hiveClient, err := hiveutils.GetClient("hiveutil-managedns-enable")
386+
hiveClient, err := utils.GetClient("hiveutil-managedns-enable")
388387
if err != nil {
389388
log.WithError(err).Error("failed to create a hive config client")
390389
return err

contrib/pkg/awsprivatelink/awsprivatelink.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ import (
1717
"sigs.k8s.io/controller-runtime/pkg/client"
1818
)
1919

20+
const (
21+
// privateLinkHubAcctCredsName is the name of the AWS PrivateLink Hub account credentials Secret
22+
// created by the "hiveutil awsprivatelink enable" command
23+
privateLinkHubAcctCredsName = "awsprivatelink-hub-acct-creds"
24+
25+
// privateLinkHubAcctCredsLabel is added to the AWS PrivateLink Hub account credentials Secret
26+
// created by the "hiveutil awsprivatelink enable" command and
27+
// referenced by HiveConfig.spec.awsPrivateLink.credentialsSecretRef.
28+
privateLinkHubAcctCredsLabel = "hive.openshift.io/awsprivatelink-hub-acct-credentials"
29+
)
30+
2031
var (
2132
logLevelDebug bool
2233
credsSecretRef string

contrib/pkg/awsprivatelink/disable.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
hivev1 "github.com/openshift/hive/apis/hive/v1"
77
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
8-
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
98
operatorutils "github.com/openshift/hive/pkg/operator/hive"
109

1110
corev1 "k8s.io/api/core/v1"
@@ -78,8 +77,8 @@ func (o *disableOptions) Run(cmd *cobra.Command, args []string) error {
7877
if err := common.DynamicClient.List(
7978
context.Background(),
8079
hubAcctSecrets,
81-
client.MatchingFields{"metadata.name": awsutils.PrivateLinkHubAcctCredsName},
82-
client.MatchingLabels{awsutils.PrivateLinkHubAcctCredsLabel: "true"},
80+
client.MatchingFields{"metadata.name": privateLinkHubAcctCredsName},
81+
client.MatchingLabels{privateLinkHubAcctCredsLabel: "true"},
8382
client.InNamespace(hiveNS),
8483
); err != nil {
8584
log.WithError(err).Error("Failed to list Hub account credentials Secrets")

contrib/pkg/awsprivatelink/enable.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
configv1 "github.com/openshift/api/config/v1"
1414
hivev1 "github.com/openshift/hive/apis/hive/v1"
1515
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
16-
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
1716
"github.com/openshift/hive/pkg/awsclient"
17+
awscreds "github.com/openshift/hive/pkg/creds/aws"
1818
operatorutils "github.com/openshift/hive/pkg/operator/hive"
1919

2020
log "github.com/sirupsen/logrus"
@@ -147,11 +147,11 @@ func (o *enableOptions) Run(cmd *cobra.Command, args []string) error {
147147

148148
switch err = common.DynamicClient.Create(context.Background(), credsSecretInHiveNS); {
149149
case err == nil:
150-
log.Infof("Secret/%s created in namespace %s", awsutils.PrivateLinkHubAcctCredsName, hiveNS)
150+
log.Infof("Secret/%s created in namespace %s", privateLinkHubAcctCredsName, hiveNS)
151151
case apierrors.IsAlreadyExists(err):
152-
log.Warnf("Secret/%s already exists in namespace %s", awsutils.PrivateLinkHubAcctCredsName, hiveNS)
152+
log.Warnf("Secret/%s already exists in namespace %s", privateLinkHubAcctCredsName, hiveNS)
153153
default:
154-
log.WithError(err).Fatalf("Failed to create Secret/%s in namespace %s", awsutils.PrivateLinkHubAcctCredsName, hiveNS)
154+
log.WithError(err).Fatalf("Failed to create Secret/%s in namespace %s", privateLinkHubAcctCredsName, hiveNS)
155155
}
156156

157157
// Update HiveConfig
@@ -194,11 +194,11 @@ func (o *enableOptions) getOrCopyCredsSecret(source *corev1.Secret, namespace st
194194
APIVersion: corev1.SchemeGroupVersion.String(),
195195
},
196196
ObjectMeta: metav1.ObjectMeta{
197-
Name: awsutils.PrivateLinkHubAcctCredsName,
197+
Name: privateLinkHubAcctCredsName,
198198
Namespace: namespace,
199199
// Secrets without this label (e.g., the ones created and configured manually) won't be deleted
200200
// when calling "hiveutil awsprivatelink disable".
201-
Labels: map[string]string{awsutils.PrivateLinkHubAcctCredsLabel: "true"},
201+
Labels: map[string]string{privateLinkHubAcctCredsLabel: "true"},
202202
},
203203
Type: corev1.SecretTypeOpaque,
204204
}
@@ -210,7 +210,7 @@ func (o *enableOptions) getOrCopyCredsSecret(source *corev1.Secret, namespace st
210210
// Get creds from environment
211211
default:
212212
defaultCredsFilePath := filepath.Join(o.homeDir, ".aws", "credentials")
213-
accessKeyID, secretAccessKey, err := awsutils.GetAWSCreds("", defaultCredsFilePath)
213+
accessKeyID, secretAccessKey, err := awscreds.GetAWSCreds("", defaultCredsFilePath)
214214
if err != nil {
215215
return nil, err
216216
}

contrib/pkg/awsprivatelink/endpointvpc/add.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
hivev1 "github.com/openshift/hive/apis/hive/v1"
1414
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
15-
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
1615
"github.com/openshift/hive/pkg/awsclient"
1716

1817
log "github.com/sirupsen/logrus"
@@ -94,7 +93,7 @@ func (o *endpointVPCAddOptions) Complete(cmd *cobra.Command, args []string) erro
9493
regions.Insert(associatedVpc.AWSPrivateLinkVPC.Region)
9594
}
9695
// Use the passed-in credsSecret if possible
97-
awsClientsByRegion, err := awsutils.GetAWSClientsByRegion(common.CredsSecret, regions)
96+
awsClientsByRegion, err := getAWSClientsByRegion(common.CredsSecret, regions)
9897
if err != nil {
9998
log.WithError(err).Fatal("Failed to get AWS clients")
10099
}
@@ -136,7 +135,7 @@ func (o *endpointVPCAddOptions) Validate(cmd *cobra.Command, args []string) erro
136135

137136
func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
138137
// Get default SG of the endpoint VPC
139-
endpointVPCDefaultSG, err := awsutils.GetDefaultSGOfVpc(o.endpointVpcClients, o.endpointVpcId)
138+
endpointVPCDefaultSG, err := getDefaultSGOfVpc(o.endpointVpcClients, o.endpointVpcId)
140139
if err != nil {
141140
log.WithError(err).Fatal("Failed to get default SG of the endpoint VPC")
142141
}
@@ -192,7 +191,7 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
192191
}
193192

194193
// Update SGs
195-
associatedVpcWorkerSG, err := awsutils.GetWorkerSGFromVpcId(associatedVpcClients, associatedVpcId)
194+
associatedVpcWorkerSG, err := getWorkerSGFromVpcId(associatedVpcClients, associatedVpcId)
196195
if err != nil {
197196
log.WithError(err).Fatal("Failed to get worker SG of the associated VPC")
198197
}
@@ -203,7 +202,7 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
203202
// Associated VPC & endpoint VPC in the same region => allow ingress from SG of the peer
204203
case associatedVpcRegion == o.endpointVpcRegion:
205204
log.Info("Authorizing traffic from the associated VPC's worker SG to the endpoint VPC's default SG")
206-
if _, err = awsutils.AuthorizeAllIngressFromSG(
205+
if _, err = authorizeAllIngressFromSG(
207206
o.endpointVpcClients,
208207
aws.String(endpointVPCDefaultSG),
209208
aws.String(associatedVpcWorkerSG),
@@ -218,7 +217,7 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
218217
}
219218

220219
log.Info("Authorizing traffic from the endpoint VPC's default SG to the associated VPC's worker SG")
221-
if _, err = awsutils.AuthorizeAllIngressFromSG(
220+
if _, err = authorizeAllIngressFromSG(
222221
associatedVpcClients,
223222
aws.String(associatedVpcWorkerSG),
224223
aws.String(endpointVPCDefaultSG),
@@ -235,7 +234,7 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
235234
// Associated VPC & endpoint VPC in different regions => allow ingress from CIDR of the peer
236235
default:
237236
log.Info("Authorizing traffic from the associated VPC's CIDR block to the endpoint VPC's default SG")
238-
if _, err = awsutils.AuthorizeAllIngressFromCIDR(
237+
if _, err = authorizeAllIngressFromCIDR(
239238
o.endpointVpcClients,
240239
aws.String(endpointVPCDefaultSG),
241240
associatedVpcCIDR,
@@ -250,7 +249,7 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
250249
}
251250

252251
log.Info("Authorizing traffic from the endpoint VPC's CIDR block to the associated VPC's worker SG")
253-
if _, err = awsutils.AuthorizeAllIngressFromCIDR(
252+
if _, err = authorizeAllIngressFromCIDR(
254253
associatedVpcClients,
255254
aws.String(associatedVpcWorkerSG),
256255
endpointVpcCIDR,
@@ -309,7 +308,7 @@ func (o *endpointVPCAddOptions) addEndpointVpcToHiveConfig() {
309308
},
310309
Subnets: endpointSubnets,
311310
}
312-
if idx, ok := awsutils.FindVpcInInventory(o.endpointVpcId, o.hiveConfig.Spec.AWSPrivateLink.EndpointVPCInventory); ok {
311+
if idx, ok := findVpcInInventory(o.endpointVpcId, o.hiveConfig.Spec.AWSPrivateLink.EndpointVPCInventory); ok {
313312
if reflect.DeepEqual(o.hiveConfig.Spec.AWSPrivateLink.EndpointVPCInventory[idx], endpointVpcToAdd) {
314313
log.Warn("Endpoint VPC found in HiveConfig. HiveConfig unchanged.")
315314
return

0 commit comments

Comments
 (0)