Skip to content

Commit

Permalink
fixed spec.network nil pointer dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
muraee committed Feb 29, 2024
1 parent 9f69479 commit a481de3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
1 change: 1 addition & 0 deletions controlplane/rosa/api/v1beta2/rosacontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type RosaControlPlaneSpec struct { //nolint: maligned
type NetworkSpec struct {
// IP addresses block used by OpenShift while installing the cluster, for example "10.0.0.0/16".
// +kubebuilder:validation:Format=cidr
// +optional
MachineCIDR string `json:"machineCIDR,omitempty"`

// IP address block from which to assign pod IP addresses, for example `10.128.0.0/14`.
Expand Down
63 changes: 33 additions & 30 deletions controlplane/rosa/controllers/rosacontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
billingAccount = rosaScope.ControlPlane.Spec.BillingAccount
}

spec := ocm.Spec{
ocmClusterSpec := ocm.Spec{
DryRun: ptr.To(false),
Name: rosaScope.RosaClusterName(),
Region: *rosaScope.ControlPlane.Spec.Region,
Expand All @@ -282,7 +282,6 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc

SubnetIds: rosaScope.ControlPlane.Spec.Subnets,
AvailabilityZones: rosaScope.ControlPlane.Spec.AvailabilityZones,
NetworkType: rosaScope.ControlPlane.Spec.Network.NetworkType,
IsSTS: true,
RoleARN: *rosaScope.ControlPlane.Spec.InstallerRoleARN,
SupportRoleARN: *rosaScope.ControlPlane.Spec.SupportRoleARN,
Expand All @@ -297,44 +296,48 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
AWSCreator: creator,
}

_, machineCIDR, err := net.ParseCIDR(rosaScope.ControlPlane.Spec.Network.MachineCIDR)
if err == nil {
spec.MachineCIDR = *machineCIDR
} else {
// TODO: expose in status
rosaScope.Error(err, "rosacontrolplane.spec.network.machineCIDR invalid", rosaScope.ControlPlane.Spec.Network.MachineCIDR)
return ctrl.Result{}, nil
}
if networkSpec := rosaScope.ControlPlane.Spec.Network; networkSpec != nil {
if networkSpec.MachineCIDR != "" {
_, machineCIDR, err := net.ParseCIDR(networkSpec.MachineCIDR)
if err != nil {
// TODO: expose in status
rosaScope.Error(err, "rosacontrolplane.spec.network.machineCIDR invalid", networkSpec.MachineCIDR)
return ctrl.Result{}, nil
}
ocmClusterSpec.MachineCIDR = *machineCIDR
}

if rosaScope.ControlPlane.Spec.Network.PodCIDR != "" {
_, podCIDR, err := net.ParseCIDR(rosaScope.ControlPlane.Spec.Network.PodCIDR)
if err == nil {
spec.PodCIDR = *podCIDR
} else {
// TODO: expose in status.
rosaScope.Error(err, "rosacontrolplane.spec.network.podCIDR invalid", rosaScope.ControlPlane.Spec.Network.PodCIDR)
return ctrl.Result{}, nil
if networkSpec.PodCIDR != "" {
_, podCIDR, err := net.ParseCIDR(networkSpec.PodCIDR)
if err != nil {
// TODO: expose in status.
rosaScope.Error(err, "rosacontrolplane.spec.network.podCIDR invalid", networkSpec.PodCIDR)
return ctrl.Result{}, nil
}
ocmClusterSpec.PodCIDR = *podCIDR
}
}

if rosaScope.ControlPlane.Spec.Network.ServiceCIDR != "" {
_, serviceCIDR, err := net.ParseCIDR(rosaScope.ControlPlane.Spec.Network.ServiceCIDR)
if err == nil {
spec.ServiceCIDR = *serviceCIDR
} else {
// TODO: expose in status.
rosaScope.Error(err, "rosacontrolplane.spec.network.serviceCIDR invalid", rosaScope.ControlPlane.Spec.Network.ServiceCIDR)
return ctrl.Result{}, nil
if networkSpec.ServiceCIDR != "" {
_, serviceCIDR, err := net.ParseCIDR(networkSpec.ServiceCIDR)
if err != nil {
// TODO: expose in status.
rosaScope.Error(err, "rosacontrolplane.spec.network.serviceCIDR invalid", networkSpec.ServiceCIDR)
return ctrl.Result{}, nil
}
ocmClusterSpec.ServiceCIDR = *serviceCIDR
}

ocmClusterSpec.HostPrefix = networkSpec.HostPrefix
ocmClusterSpec.NetworkType = networkSpec.NetworkType
}

// Set autoscale replica
if rosaScope.ControlPlane.Spec.Autoscaling != nil {
spec.MaxReplicas = rosaScope.ControlPlane.Spec.Autoscaling.MaxReplicas
spec.MinReplicas = rosaScope.ControlPlane.Spec.Autoscaling.MinReplicas
ocmClusterSpec.MaxReplicas = rosaScope.ControlPlane.Spec.Autoscaling.MaxReplicas
ocmClusterSpec.MinReplicas = rosaScope.ControlPlane.Spec.Autoscaling.MinReplicas
}

cluster, err = ocmClient.CreateCluster(spec)
cluster, err = ocmClient.CreateCluster(ocmClusterSpec)
if err != nil {
// TODO: need to expose in status, as likely the spec is invalid
return ctrl.Result{}, fmt.Errorf("failed to create OCM cluster: %w", err)
Expand Down

0 comments on commit a481de3

Please sign in to comment.