Skip to content

Commit f725cf0

Browse files
authored
Always public IAM endpoint for satellite cluster (#22)
* Always public IAM endpoint for satellite cluster * Addressing comments - reducing redundancy * Addressing comments - using cluster info to determine cluster provider * Always use private URL, if unable to determine satellite * Read URL provided in storage secret store for satellite
1 parent 9044589 commit f725cf0

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

pkg/config/cluster_config.go

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package config
1818

1919
import (
2020
"encoding/json"
21-
"os"
2221
"strings"
2322

2423
"github.com/IBM/secret-utils-lib/pkg/k8s_utils"
@@ -41,8 +40,9 @@ const (
4140

4241
// ClusterConfig ...
4342
type ClusterConfig struct {
44-
ClusterID string `json:"cluster_id"`
45-
MasterURL string `json:"master_url"`
43+
ClusterID string `json:"cluster_id"`
44+
MasterURL string `json:"master_url"`
45+
ClusterProvider string `json:"cluster_provider"`
4646
}
4747

4848
// GetClusterInfo ...
@@ -72,29 +72,31 @@ func FrameTokenExchangeURL(kc k8s_utils.KubernetesClient, providerType string, l
7272
return cloudConf.TokenExchangeURL + tokenExchangePath
7373
}
7474

75+
cc, err := GetClusterInfo(kc, logger)
76+
if err != nil {
77+
logger.Error("Error fetching cluster info", zap.Error(err))
78+
return (utils.ProdPrivateIAMURL + tokenExchangePath)
79+
}
80+
81+
isSatellite := IsSatellite(cc, logger)
82+
7583
logger.Info("Unable to fetch token exchange URL from cloud-conf")
7684
secret, err := k8s_utils.GetSecretData(kc, utils.STORAGE_SECRET_STORE_SECRET, utils.SECRET_STORE_FILE)
7785
if err == nil {
7886
if secretConfig, err := ParseConfig(logger, secret); err == nil {
79-
url, err := GetTokenExchangeURLfromStorageSecretStore(*secretConfig, providerType)
87+
url, err := GetTokenExchangeURLfromStorageSecretStore(isSatellite, *secretConfig, providerType)
8088
if err == nil {
8189
return url
8290
}
8391
}
8492
}
8593

8694
logger.Info("Unable to fetch token exchange URL using secret, forming url using cluster info")
87-
cc, err := GetClusterInfo(kc, logger)
88-
if err != nil {
89-
logger.Error("Error fetching cluster master URL", zap.Error(err))
90-
return (utils.ProdPublicIAMURL + tokenExchangePath)
91-
}
92-
93-
return FrameTokenExchangeURLFromClusterInfo(cc, logger)
95+
return FrameTokenExchangeURLFromClusterInfo(isSatellite, cc, logger)
9496
}
9597

9698
// GetTokenExchangeURLfromStorageSecretStore ...
97-
func GetTokenExchangeURLfromStorageSecretStore(config Config, providerType string) (string, error) {
99+
func GetTokenExchangeURLfromStorageSecretStore(isSatellite bool, config Config, providerType string) (string, error) {
98100

99101
var url string
100102
switch providerType {
@@ -110,28 +112,49 @@ func GetTokenExchangeURLfromStorageSecretStore(config Config, providerType strin
110112
return "", utils.Error{Description: utils.WarnFetchingTokenExchangeURL}
111113
}
112114

113-
// If the cluster is not satellite cluster, use PROD or STAGE URLs
114-
if os.Getenv("IS_SATELLITE") != constTrue {
115-
if !strings.Contains(url, "stage") && !strings.Contains(url, "test") {
116-
url = utils.ProdPrivateIAMURL
117-
} else {
118-
url = utils.StagePrivateIAMURL
119-
}
115+
// If the cluster is satellite, first use the provided URL.
116+
if isSatellite {
117+
return url, nil
120118
}
121119

122-
// Appending the base URL and token exchange path
123-
url = url + tokenExchangePath
124-
125-
return url, nil
120+
isProd := isProduction(url)
121+
if isProd {
122+
return utils.ProdPrivateIAMURL + tokenExchangePath, nil
123+
}
124+
return utils.StagePrivateIAMURL + tokenExchangePath, nil
126125
}
127126

128127
// FrameTokenExchangeURLFromClusterInfo ...
129-
func FrameTokenExchangeURLFromClusterInfo(cc ClusterConfig, logger *zap.Logger) string {
128+
func FrameTokenExchangeURLFromClusterInfo(isSatellite bool, cc ClusterConfig, logger *zap.Logger) string {
129+
130130
if !strings.Contains(cc.MasterURL, stageMasterURLsubstr) {
131131
logger.Info("Env-Production")
132+
if isSatellite {
133+
return (utils.ProdPublicIAMURL + tokenExchangePath)
134+
}
132135
return (utils.ProdPrivateIAMURL + tokenExchangePath)
133136
}
134137

135138
logger.Info("Env-Stage")
139+
if isSatellite {
140+
return (utils.StagePublicIAMURL + tokenExchangePath)
141+
}
136142
return (utils.StagePrivateIAMURL + tokenExchangePath)
137143
}
144+
145+
// isProduction determines if the env in which a pod is deployed is stage or production
146+
func isProduction(url string) bool {
147+
if !strings.Contains(url, "stage") && !strings.Contains(url, "test") {
148+
return true
149+
}
150+
return false
151+
}
152+
153+
// IsSatellite checks if the cluster where the pod is currently running is a satellite cluster or not
154+
func IsSatellite(cc ClusterConfig, logger *zap.Logger) bool {
155+
if cc.ClusterProvider == utils.SatelliteProvider {
156+
return true
157+
}
158+
159+
return false
160+
}

pkg/utils/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ const (
5555
Bluemix = "bluemix"
5656
// Softlayer ...
5757
Softlayer = "softlayer"
58+
// SatelliteProvider ...
59+
SatelliteProvider = "upi"
5860
)

0 commit comments

Comments
 (0)