Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
Allow default project-id and environment
Browse files Browse the repository at this point in the history
This change makes it easier to use this provider when the entire cluster
is using a single cloudstack project in a single cloudstack enviroment.

The environment and project id labels in each node are no longer
required in this situation.
  • Loading branch information
cezarsa committed Jul 29, 2020
1 parent d829138 commit 8beed3c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions cloudstack/cloudstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type environmentConfig struct {
SecretKey string `gcfg:"secret-key"`
LBEnvironmentID string `gcfg:"lb-environment-id"`
LBDomain string `gcfg:"lb-domain"`
ProjectID string `gcfg:"project-id"`
SSLNoVerify bool `gcfg:"ssl-no-verify"`
RemoveLBs bool `gcfg:"remove-lbs-on-delete"`
}
Expand Down
31 changes: 27 additions & 4 deletions cloudstack/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ func (cs *CSCloud) newNode(kubeNode *v1.Node) (*node, error) {
if n, ok := getLabelOrAnnotation(kubeNode.ObjectMeta, cs.config.Global.NodeNameLabel); ok {
name = n
}
environment, _ := getLabelOrAnnotation(kubeNode.ObjectMeta, cs.config.Global.EnvironmentLabel)
projectID, ok := getLabelOrAnnotation(kubeNode.ObjectMeta, cs.config.Global.ProjectIDLabel)
if !ok {
return nil, fmt.Errorf("failed to retrieve projectID from node %#v", kubeNode)
environment := cs.environmentForMeta(kubeNode.ObjectMeta)
projectID, err := cs.projectForMeta(kubeNode.ObjectMeta, environment)
if err != nil {
return nil, fmt.Errorf("error processing node %#v: %v", kubeNode, err)
}
n := &node{
projectID: projectID,
Expand All @@ -313,6 +313,29 @@ func (cs *CSCloud) clientForNode(n *node) (*cloudstack.CloudStackClient, error)
return cs.clientForEnvironment(n.environment)
}

func (cs *CSCloud) projectForMeta(meta metav1.ObjectMeta, environment string) (string, error) {
projectID, ok := getLabelOrAnnotation(meta, cs.config.Global.ProjectIDLabel)
if !ok {
if envConfig, ok := cs.config.Environment[environment]; ok {
projectID = envConfig.ProjectID
}
if projectID == "" {
return "", errors.New("no projectID found")
}
}
return projectID, nil
}

func (cs *CSCloud) environmentForMeta(meta metav1.ObjectMeta) string {
environment, _ := getLabelOrAnnotation(meta, cs.config.Global.EnvironmentLabel)
if environment == "" && len(cs.environments) == 1 {
for k := range cs.environments {
environment = k
}
}
return environment
}

func (cs *CSCloud) clientForEnvironment(environment string) (*cloudstack.CloudStackClient, error) {
client := cs.environments[environment].client
if client == nil {
Expand Down
23 changes: 11 additions & 12 deletions cloudstack/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
return nil, err
}

if lb.cloud.projectID != "" && service.Labels[cs.config.Global.ProjectIDLabel] == "" {
if lb.cloud.projectID != "" && cs.config.Global.ProjectIDLabel != "" && service.Labels[cs.config.Global.ProjectIDLabel] == "" {
service.Labels[cs.config.Global.ProjectIDLabel] = lb.cloud.projectID

_, err = cs.kubeClient.CoreV1().Services(service.Namespace).Patch(
Expand Down Expand Up @@ -391,14 +391,14 @@ func isLBRemovalEnabled(lb *loadBalancer, service *v1.Service) bool {

// getLoadBalancer retrieves the IP address and ID and all the existing rules it can find.
func (cs *CSCloud) getLoadBalancer(service *v1.Service, projectID string, networkIDs []string) (*loadBalancer, error) {
environment := cs.environmentForMeta(service.ObjectMeta)
if projectID == "" {
var ok bool
projectID, ok = getLabelOrAnnotation(service.ObjectMeta, cs.config.Global.ProjectIDLabel)
if !ok {
klog.V(4).Infof("unable to retrieve projectID for service: %#v", service)
var err error
projectID, err = cs.projectForMeta(service.ObjectMeta, environment)
if err != nil {
klog.V(4).Infof("unable to retrieve projectID for service: %#v: %v", service, err)
}
}
environment, _ := getLabelOrAnnotation(service.ObjectMeta, cs.config.Global.EnvironmentLabel)
lb := &loadBalancer{
cloud: &projectCloud{
CSCloud: cs,
Expand Down Expand Up @@ -534,11 +534,10 @@ func (cs *CSCloud) extractIDs(nodes []*v1.Node) ([]string, []string, string, err
return nil, nil, "", errors.New("no nodes available to add to load balancer")
}

environmentID, _ := getLabelOrAnnotation(nodes[0].ObjectMeta, cs.config.Global.EnvironmentLabel)

projectID, ok := getLabelOrAnnotation(nodes[0].ObjectMeta, cs.config.Global.ProjectIDLabel)
if !ok {
return nil, nil, "", fmt.Errorf("unable to retrieve projectID for node %#v", nodes[0])
environmentID := cs.environmentForMeta(nodes[0].ObjectMeta)
projectID, err := cs.projectForMeta(nodes[0].ObjectMeta, environmentID)
if err != nil {
return nil, nil, "", fmt.Errorf("unable to retrieve projectID for node %#v: %v", nodes[0], err)
}

var manager *cloudstackManager
Expand Down Expand Up @@ -635,7 +634,7 @@ func (cs *CSCloud) getLoadBalancerName(service *v1.Service) string {
if suffix != "" {
return fmt.Sprintf("%s.%s", service.Name, suffix)
}
environment, _ := getLabelOrAnnotation(service.ObjectMeta, cs.config.Global.EnvironmentLabel)
environment := cs.environmentForMeta(service.ObjectMeta)
var lbDomain string
if env, ok := cs.environments[environment]; ok {
lbDomain = env.lbDomain
Expand Down

0 comments on commit 8beed3c

Please sign in to comment.