Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joekr committed May 18, 2022
1 parent 1b8503a commit 97a8088
Show file tree
Hide file tree
Showing 19 changed files with 373 additions and 329 deletions.
15 changes: 15 additions & 0 deletions api/v1beta1/ocicluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ type OCIClusterStatus struct {
// +optional
FailureDomains clusterv1.FailureDomains `json:"failureDomains,omitempty"`

// AvailabilityDomains encapsulates the clusters Availability Domain (AD) information in a map
// where the map key is the AD name and the struct is details about the AD.
// +optional
AvailabilityDomains map[string]OCIAvailabilityDomain `json:"availabilityDomains,omitempty"`

// +optional
Ready bool `json:"ready"`
// NetworkSpec encapsulates all things related to OCI network.
Expand Down Expand Up @@ -99,6 +104,16 @@ type OCIClusterList struct {
Items []OCICluster `json:"items"`
}

// OCIAvailabilityDomain contains information about an Availability Domain (AD).
type OCIAvailabilityDomain struct {

// Name is the AD's full name. Example: Uocm:PHX-AD-1
Name string `json:"name,omitempty"`

// FaultDomains a list of fault domain (FD) names. Example: ["FAULT-DOMAIN-1"]
FaultDomains []string `json:"faultDomains,omitempty"`
}

// GetConditions returns the list of conditions for an OCICluster API object.
func (c *OCICluster) GetConditions() clusterv1.Conditions {
return c.Status.Conditions
Expand Down
27 changes: 27 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions cloud/scope/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package scope

import (
"fmt"
"sync"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -90,7 +89,6 @@ func (c *ClientProvider) GetOrBuildClient(region string) (OCIClients, error) {
return regionalClient, err
}
c.ociClients[region] = regionalClient
fmt.Println("------ regional client", regionalClient.ComputeManagementClient)

return regionalClient, nil
}
Expand All @@ -106,7 +104,6 @@ func createClients(region string, oCIAuthConfigProvider common.ConfigurationProv
return OCIClients{}, err
}

fmt.Println("------ computeManagementClient", computeManagementClient)
return OCIClients{
VCNClient: vcnClient,
LoadBalancerClient: lbClient,
Expand Down
50 changes: 45 additions & 5 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"reflect"
"sigs.k8s.io/cluster-api/util/conditions"
"strconv"

"github.com/oracle/cluster-api-provider-oci/cloud/services/vcn"
Expand Down Expand Up @@ -108,6 +109,7 @@ func NewClusterScope(params ClusterScopeParams) (*ClusterScope, error) {

// PatchObject persists the cluster configuration and status.
func (s *ClusterScope) PatchObject(ctx context.Context) error {
conditions.SetSummary(s.OCICluster)
return s.patchHelper.Patch(ctx, s.OCICluster)
}

Expand Down Expand Up @@ -137,23 +139,29 @@ func (s *ClusterScope) IsResourceCreatedByClusterAPI(resourceFreeFormTags map[st
// in case of single AD regions, the failure domain will be fault domain, in case of multi Ad regions, it will
// be AD
func (s *ClusterScope) setFailureDomains(ctx context.Context) error {
req := identity.ListAvailabilityDomainsRequest{CompartmentId: common.String(s.GetCompartmentId())}
reqAd := identity.ListAvailabilityDomainsRequest{CompartmentId: common.String(s.GetCompartmentId())}

resp, err := s.IdentityClient.ListAvailabilityDomains(ctx, req)
respAd, err := s.IdentityClient.ListAvailabilityDomains(ctx, reqAd)
if err != nil {
s.Logger.Error(err, "failed to list identity domains")
return err
}

numOfAds := len(resp.Items)
// build the AD list for cluster
err = s.setAvailabiltyDomainStatus(ctx, respAd.Items)
if err != nil {
return err
}

numOfAds := len(respAd.Items)
if numOfAds != 1 && numOfAds != 3 {
err := errors.New(fmt.Sprintf("invalid number of Availability Domains, should be either 1 or 3, but got %d", numOfAds))
s.Logger.Error(err, "invalid number of Availability Domains")
return err
}

if numOfAds == 3 {
for i, ad := range resp.Items {
for i, ad := range respAd.Items {
s.SetFailureDomain(strconv.Itoa(i+1), clusterv1.FailureDomainSpec{
ControlPlane: true,
Attributes: map[string]string{AvailabilityDomain: *ad.Name},
Expand All @@ -162,7 +170,7 @@ func (s *ClusterScope) setFailureDomains(ctx context.Context) error {
} else {
req := identity.ListFaultDomainsRequest{
CompartmentId: common.String(s.GetCompartmentId()),
AvailabilityDomain: resp.Items[0].Name,
AvailabilityDomain: respAd.Items[0].Name,
}
resp, err := s.IdentityClient.ListFaultDomains(ctx, req)
if err != nil {
Expand Down Expand Up @@ -191,6 +199,38 @@ func (s *ClusterScope) SetFailureDomain(id string, spec clusterv1.FailureDomainS
s.OCICluster.Status.FailureDomains[id] = spec
}

// setAvailabiltyDomainStatus builds the OCIAvailabilityDomain list and sets the OCICluster's status with this list
// so that other parts of the provider have access to ADs and FDs without having to make multiple calls to identity.
func (s *ClusterScope) setAvailabiltyDomainStatus(ctx context.Context, ads []identity.AvailabilityDomain) error {
clusterAds := make(map[string]infrastructurev1beta1.OCIAvailabilityDomain)
for _, ad := range ads {
reqFd := identity.ListFaultDomainsRequest{
CompartmentId: common.String(s.GetCompartmentId()),
AvailabilityDomain: ad.Name,
}
respFd, err := s.IdentityClient.ListFaultDomains(ctx, reqFd)
if err != nil {
s.Logger.Error(err, "failed to list fault domains")
return err
}

var faultDomains []string
for _, fd := range respFd.Items {
faultDomains = append(faultDomains, *fd.Name)
}

adName := *ad.Name
clusterAds[adName] = infrastructurev1beta1.OCIAvailabilityDomain{
Name: adName,
FaultDomains: faultDomains,
}
}

s.OCICluster.Status.AvailabilityDomains = clusterAds

return nil
}

func (s *ClusterScope) IsTagsEqual(freeFromTags map[string]string, definedTags map[string]map[string]interface{}) bool {
if reflect.DeepEqual(freeFromTags, s.GetFreeFormTags()) && reflect.DeepEqual(definedTags, s.GetDefinedTags()) {
return true
Expand Down
2 changes: 2 additions & 0 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/base64"
"fmt"
"math/rand"
"sigs.k8s.io/cluster-api/util/conditions"
"strconv"
"time"

Expand Down Expand Up @@ -330,6 +331,7 @@ func (m *MachineScope) GetMachineByDisplayName(ctx context.Context, name string)

// PatchObject persists the cluster configuration and status.
func (m *MachineScope) PatchObject(ctx context.Context) error {
conditions.SetSummary(m.OCIMachine)
return m.patchHelper.Patch(ctx, m.OCIMachine)
}

Expand Down
Loading

0 comments on commit 97a8088

Please sign in to comment.