Skip to content

Commit

Permalink
Created a new env var for metal to replace/support packet env vars as…
Browse files Browse the repository at this point in the history
… usual
  • Loading branch information
aayushrangwala committed Sep 15, 2023
1 parent a707cdc commit 8f5d591
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ spec:
secretKeyRef:
name: bootstrap-token-cluster-autoscaler-packet
key: token-secret
- name: PACKET_AUTH_TOKEN
- name: METAL_AUTH_TOKEN
valueFrom:
secretKeyRef:
name: cluster-autoscaler-packet
Expand Down
18 changes: 13 additions & 5 deletions cluster-autoscaler/cloudprovider/packet/packet_cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const (
// master/controller node.
DefaultControllerNodeLabelKey = "node-role.kubernetes.io/master"
// ControllerNodeIdentifierEnv is the string for the environment variable.
ControllerNodeIdentifierEnv = "PACKET_CONTROLLER_NODE_IDENTIFIER_LABEL"
// Deprecated: This env var is deprecated in the favour packet's acquisition to equinix.
// Please use 'ControllerNodeIdentifierMetalEnv'
ControllerNodeIdentifierEnv = "PACKET_CONTROLLER_NODE_IDENTIFIER_LABEL"
ControllerNodeIdentifierMetalEnv = "METAL_CONTROLLER_NODE_IDENTIFIER_LABEL"
)

var (
Expand Down Expand Up @@ -106,10 +109,15 @@ func (pcp *equinixMetalCloudProvider) AddNodeGroup(group equinixMetalNodeGroup)
//
// Since only a single node group is currently supported, the first node group is always returned.
func (pcp *equinixMetalCloudProvider) NodeGroupForNode(node *apiv1.Node) (cloudprovider.NodeGroup, error) {
controllerNodeLabel := os.Getenv(ControllerNodeIdentifierEnv)
if controllerNodeLabel == "" {
klog.V(3).Infof("env %s not set, using default: %s", ControllerNodeIdentifierEnv, DefaultControllerNodeLabelKey)
controllerNodeLabel = DefaultControllerNodeLabelKey
controllerNodeLabel := DefaultControllerNodeLabelKey
value, present := os.LookupEnv(ControllerNodeIdentifierMetalEnv)
if present {
controllerNodeLabel = value
} else {
controllerNodeLabel = os.Getenv(ControllerNodeIdentifierEnv)
if controllerNodeLabel == "" {
klog.V(3).Infof("env %s not set, using default: %s", ControllerNodeIdentifierEnv, DefaultControllerNodeLabelKey)
}
}

if _, found := node.ObjectMeta.Labels[controllerNodeLabel]; found {
Expand Down
13 changes: 10 additions & 3 deletions cluster-autoscaler/cloudprovider/packet/packet_manager_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
userAgent = "kubernetes/cluster-autoscaler/" + version.ClusterAutoscalerVersion
expectedAPIContentTypePrefix = "application/json"
prefix = "equinixmetal://"
metalAuthTokenEnv = "METAL_AUTH_TOKEN"
)

type instanceType struct {
Expand Down Expand Up @@ -298,9 +299,15 @@ func createEquinixMetalManagerRest(configReader io.Reader, discoverOpts cloudpro
klog.Fatalf("No \"default\" or [Global] nodepool definition was found")
}

metalAuthToken := os.Getenv("PACKET_AUTH_TOKEN")
if len(metalAuthToken) == 0 {
klog.Fatalf("PACKET_AUTH_TOKEN is required and missing")
var metalAuthToken string
value, present := os.LookupEnv(metalAuthTokenEnv)
if present {
metalAuthToken = value
} else {
metalAuthToken = os.Getenv("PACKET_AUTH_TOKEN")
if len(metalAuthToken) == 0 {
klog.Fatalf("%s or PACKET_AUTH_TOKEN is required and missing", metalAuthTokenEnv)
}
}

manager.authToken = metalAuthToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestListMetalDevices(t *testing.T) {
var m *equinixMetalManagerRest
server := NewHttpServerMock(MockFieldContentType, MockFieldResponse)
defer server.Close()
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 {
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 || len(os.Getenv(metalAuthTokenEnv)) > 0 {
// If auth token set in env, hit the actual Packet API
m = newTestPacketManagerRest(t, "https://api.equinix.com/metal/v1/")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestIncreaseDecreaseSize(t *testing.T) {
server := NewHttpServerMock(MockFieldContentType, MockFieldResponse)
defer server.Close()
assert.Equal(t, true, true)
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 {
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 || len(os.Getenv(metalAuthTokenEnv)) > 0 {
// If auth token set in env, hit the actual Packet API
m = newTestPacketManagerRest(t, "https://api.equinix.com")
} else {
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestIncreaseDecreaseSize(t *testing.T) {
err = ngPool3.IncreaseSize(1)
assert.NoError(t, err)

if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 {
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 || len(os.Getenv(metalAuthTokenEnv)) > 0 {
// If testing with actual API give it some time until the nodes bootstrap
time.Sleep(420 * time.Second)
}
Expand All @@ -120,7 +120,7 @@ func TestIncreaseDecreaseSize(t *testing.T) {
err = ngPool2.IncreaseSize(1)
assert.NoError(t, err)

if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 {
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 || len(os.Getenv(metalAuthTokenEnv)) > 0 {
// If testing with actual API give it some time until the nodes bootstrap
time.Sleep(420 * time.Second)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestIncreaseDecreaseSize(t *testing.T) {
assert.NoError(t, err)

// Wait a few seconds if talking to the actual Packet API
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 {
if len(os.Getenv("PACKET_AUTH_TOKEN")) > 0 || len(os.Getenv(metalAuthTokenEnv)) > 0 {
time.Sleep(10 * time.Second)
}

Expand Down

0 comments on commit 8f5d591

Please sign in to comment.