Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joekr committed May 17, 2022
1 parent 1b8503a commit 2e016ec
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 213 deletions.
Empty file added auth-config.yaml
Empty file.
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
2 changes: 2 additions & 0 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
9 changes: 9 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,7 +331,15 @@ 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)

//return m.patchHelper.Patch(
// ctx,
// m.OCIMachine,
// patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
// clusterv1.ReadyCondition,
//// }})
}

// Close closes the current scope persisting the cluster configuration and status.
Expand Down
126 changes: 55 additions & 71 deletions cloud/scope/machine_pool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021, 2022 Oracle and/or its affiliates.
Copyright (c) 2022 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package scope
import (
"context"
"fmt"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"strconv"

"github.com/oracle/cluster-api-provider-oci/cloud/ociutil"
Expand All @@ -35,47 +36,35 @@ import (

"github.com/go-logr/logr"
infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
"github.com/oracle/cluster-api-provider-oci/cloud/services/compute"
"github.com/oracle/cluster-api-provider-oci/cloud/services/computemanagement"
nlb "github.com/oracle/cluster-api-provider-oci/cloud/services/networkloadbalancer"
"github.com/oracle/cluster-api-provider-oci/cloud/services/vcn"
"github.com/pkg/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const OCIMachinePoolKind = "OCIMachinePool"

// MachineScopeParams defines the params need to create a new MachineScope
type MachinePoolScopeParams struct {
Logger *logr.Logger
Cluster *clusterv1.Cluster
MachinePool *expclusterv1.MachinePool
Client client.Client
ComputeClient compute.ComputeClient
ComputeManagementClient computemanagement.Client
OCICluster *infrastructurev1beta1.OCICluster
OCIMachinePool *expinfra1.OCIMachinePool
VCNClient vcn.Client
NetworkLoadBalancerClient nlb.NetworkLoadBalancerClient
Machine *clusterv1.Machine
OCIMachine *infrastructurev1beta1.OCIMachine
Logger *logr.Logger
Cluster *clusterv1.Cluster
MachinePool *expclusterv1.MachinePool
Client client.Client
ComputeManagementClient computemanagement.Client
OCICluster *infrastructurev1beta1.OCICluster
OCIMachinePool *expinfra1.OCIMachinePool
OCIMachine *infrastructurev1beta1.OCIMachine
}

type MachinePoolScope struct {
*logr.Logger
Client client.Client
patchHelper *patch.Helper
Cluster *clusterv1.Cluster
MachinePool *expclusterv1.MachinePool
ComputeClient compute.ComputeClient
ComputeManagementClient computemanagement.Client
OCICluster *infrastructurev1beta1.OCICluster
OCIMachinePool *expinfra1.OCIMachinePool
VCNClient vcn.Client
NetworkLoadBalancerClient nlb.NetworkLoadBalancerClient
Machine *clusterv1.Machine
OCIMachine *infrastructurev1beta1.OCIMachine
Client client.Client
patchHelper *patch.Helper
Cluster *clusterv1.Cluster
MachinePool *expclusterv1.MachinePool
ComputeManagementClient computemanagement.Client
OCICluster *infrastructurev1beta1.OCICluster
OCIMachinePool *expinfra1.OCIMachinePool
OCIMachine *infrastructurev1beta1.OCIMachine
}

// NewMachinePoolScope creates a MachinePoolScope given the MachinePoolScopeParams
Expand All @@ -97,18 +86,14 @@ func NewMachinePoolScope(params MachinePoolScopeParams) (*MachinePoolScope, erro
}

return &MachinePoolScope{
Logger: params.Logger,
Client: params.Client,
ComputeClient: params.ComputeClient,
ComputeManagementClient: params.ComputeManagementClient,
Cluster: params.Cluster,
OCICluster: params.OCICluster,
patchHelper: helper,
MachinePool: params.MachinePool,
OCIMachinePool: params.OCIMachinePool,
VCNClient: params.VCNClient,
NetworkLoadBalancerClient: params.NetworkLoadBalancerClient,
Machine: params.Machine,
Logger: params.Logger,
Client: params.Client,
ComputeManagementClient: params.ComputeManagementClient,
Cluster: params.Cluster,
OCICluster: params.OCICluster,
patchHelper: helper,
MachinePool: params.MachinePool,
OCIMachinePool: params.OCIMachinePool,
}, nil
}

Expand Down Expand Up @@ -152,11 +137,10 @@ func (m *MachinePoolScope) SetReady() {
m.OCIMachinePool.Status.Ready = true
}

// GetWorkerMachineSubnet returns the WorkerRole core.Subnet id for the cluster
func (m *MachinePoolScope) GetWorkerMachineSubnet() *string {
for _, subnet := range m.OCICluster.Spec.NetworkSpec.Vcn.Subnets {
fmt.Println("---- subnet", subnet.Name)
if subnet.Role == infrastructurev1beta1.WorkerRole {
// if a subnet name is defined, use the correct subnet
return subnet.ID
}
}
Expand All @@ -182,17 +166,19 @@ func (m *MachinePoolScope) GetBootstrapData() (string, error) {
return string(value), nil
}

// GetWorkerMachineNSG returns the worker role core.NetworkSecurityGroup id for the cluster
func (m *MachinePoolScope) GetWorkerMachineNSG() *string {
for _, nsg := range m.OCICluster.Spec.NetworkSpec.Vcn.NetworkSecurityGroups {
if nsg.Role == infrastructurev1beta1.WorkerRole {
// if an NSG name is defined, use the correct NSG
return nsg.ID
}
}
return nil
}

func (m *MachinePoolScope) BuildInstanceConfirgurationShapeConfig() (core.InstanceConfigurationLaunchInstanceShapeConfigDetails, error) {
// BuildInstanceConfigurationShapeConfig builds the core.InstanceConfigurationLaunchInstanceShapeConfigDetails based
// on the MachinePoolScope
func (m *MachinePoolScope) BuildInstanceConfigurationShapeConfig() (core.InstanceConfigurationLaunchInstanceShapeConfigDetails, error) {
shapeConfig := core.InstanceConfigurationLaunchInstanceShapeConfigDetails{}
if (m.OCIMachinePool.Spec.ShapeConfig != expinfra1.ShapeConfig{}) {
ocpuString := m.OCIMachinePool.Spec.ShapeConfig.Ocpus
Expand Down Expand Up @@ -227,8 +213,6 @@ func (m *MachinePoolScope) BuildInstanceConfirgurationShapeConfig() (core.Instan
return shapeConfig, nil
}

// TODO: pull the following funcs out into different file

// IsResourceCreatedByClusterAPI determines if the instance was created by the cluster using the
// tags created at instance launch.
func (s *MachinePoolScope) IsResourceCreatedByClusterAPI(resourceFreeFormTags map[string]string) bool {
Expand All @@ -241,32 +225,32 @@ func (s *MachinePoolScope) IsResourceCreatedByClusterAPI(resourceFreeFormTags ma
return true
}

// GetInstanceConfigurationByDisplayName returns the existing LaunchTemplate or nothing if it doesn't exist.
// For now by name until we need the input to be something different.
func (m *MachinePoolScope) GetInstanceConfigurationIdBy(ctx context.Context) (string, error) {
req := core.ListInstanceConfigurationsRequest{
SortBy: core.ListInstanceConfigurationsSortByDisplayname,
CompartmentId: common.String(m.OCICluster.Spec.CompartmentId)}
// TODO: will want to paginate to make sure we hit all the configurations (testing now assumes very few configs in compartment)
resp, err := m.ComputeManagementClient.ListInstanceConfigurations(ctx, req)
if err != nil {
return "", err
}
if len(resp.Items) == 0 {
return "", nil
}
for _, instance := range resp.Items {
if m.IsResourceCreatedByClusterAPI(instance.FreeformTags) {
return *instance.Id, nil
}
}
return "", nil
}

// TODO: unexport this later
//
//// GetInstanceConfigurationByDisplayName returns the existing core.InstanceConfiguration id if the resource
//// was created by the cluster
//func (m *MachinePoolScope) GetInstanceConfigurationIdBy(ctx context.Context) (string, error) {
// req := core.ListInstanceConfigurationsRequest{
// SortBy: core.ListInstanceConfigurationsSortByDisplayname,
// CompartmentId: common.String(m.OCICluster.Spec.CompartmentId)}
//
// resp, err := m.ComputeManagementClient.ListInstanceConfigurations(ctx, req)
// if err != nil {
// return "", err
// }
// if len(resp.Items) == 0 {
// return "", nil
// }
// for _, instance := range resp.Items {
// if m.IsResourceCreatedByClusterAPI(instance.FreeformTags) {
// return *instance.Id, nil
// }
// }
// return "", nil
//}

// GetFreeFormTags gets the free form tags for the MachinePoolScope cluster and returns them
func (m *MachinePoolScope) GetFreeFormTags(ociCluster infrastructurev1beta1.OCICluster) map[string]string {
tags := ociutil.BuildClusterTags(ociCluster.GetOCIResourceIdentifier())
// first use cluster level tags, then override with machine level tags
if ociCluster.Spec.FreeformTags != nil {
for k, v := range ociCluster.Spec.FreeformTags {
tags[k] = v
Expand Down
2 changes: 0 additions & 2 deletions cloud/services/computemanagement/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ type Client interface {
GetInstanceConfiguration(ctx context.Context, request core.GetInstanceConfigurationRequest) (response core.GetInstanceConfigurationResponse, err error)
ListInstanceConfigurations(ctx context.Context, request core.ListInstanceConfigurationsRequest) (response core.ListInstanceConfigurationsResponse, err error)
DeleteInstanceConfiguration(ctx context.Context, request core.DeleteInstanceConfigurationRequest) (response core.DeleteInstanceConfigurationResponse, err error)
//UpdateInstanceConfiguration
//GetInstanceConfiguration
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ spec:
instanceConfiguration:
properties:
instanceConfigurationId:
description: displayName freeformTags
type: string
instanceDetails:
properties:
Expand Down
20 changes: 0 additions & 20 deletions exp/api/v1beta1/ocimachinepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,12 @@ type OCIMachinePoolSpec struct {
// Ad
// subnet
// tags
// MachineTemplateSpec
// MachineDeploymentStrategy
// ProviderIDList

InstanceConfiguration InstanceConfiguration `json:"instanceConfiguration,omitempty"`
}

type InstanceConfiguration struct {
// displayName
//freeformTags
InstanceConfigurationId *string `json:"instanceConfigurationId,omitempty"`
InstanceDetails InstanceDetails `json:"instanceDetails,omitempty"`
}
Expand All @@ -86,11 +82,6 @@ type InstanceDetails struct {
// LaunchDetails Instance launch details for creating an instance from an instance configuration
// https://docs.oracle.com/en-us/iaas/api/#/en/iaas/20160918/datatypes/InstanceConfigurationLaunchInstanceDetails
type LaunchDetails struct {
//availabilityDomain
//displayName
//freeformTags
//shapeConfig

// Custom metadata key/value pairs that you provide, such as the SSH public key
// required to connect to the instance.
Metadata map[string]string `json:"metadata,omitempty"`
Expand All @@ -117,15 +108,9 @@ type ShapeConfig struct {
BaselineOcpuUtilization string `json:"baselineOcpuUtilization,omitempty"`
}

// https://docs.oracle.com/en-us/iaas/api/#/en/iaas/20160918/datatypes/InstanceConfigurationLaunchInstanceShapeConfigDetails
// type ShapeConfigDetails struct {
// }

// SourceDetails source details for instance launched from instance configuration
// https://docs.oracle.com/en-us/iaas/api/#/en/iaas/20160918/datatypes/InstanceConfigurationInstanceSourceViaImageDetails
type SourceDetails struct {
// bootVolumeSizeInGBs

// OCID of the image to be used to launch the instance
ImageId string `json:"imageId,omitempty"`
}
Expand All @@ -143,11 +128,6 @@ type OCIMachinePoolStatus struct {
// The ID of the Instance Configuration
InstanceConfigurationId string `json:"instanceConfigurationId,omitempty"`

// Todo
// ReadyReplicas
// UnavailableReplicas
// InfrastructureReady

// Conditions defines current service state of the OCIMachinePool.
// +optional
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
Expand Down
Loading

0 comments on commit 2e016ec

Please sign in to comment.