Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add condition helpers to Multi-Network API. #526

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions crd/apis/network/v1/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package v1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ###############
// ### Network ###

// NetworkConditionType is the type for status conditions on
// a Network. This type should be used with the
// NetworkStatus.Conditions field.
type NetworkConditionType string

const (
// NetworkConditionStatusReady is the condition type that holds
// if the Network object is validated
NetworkConditionStatusReady NetworkConditionType = "Ready"

// NetworkConditionStatusParamsReady is the condition type that holds
// if the params object referenced by Network is validated
NetworkConditionStatusParamsReady NetworkConditionType = "ParamsReady"
)

// NetworkReadyConditionReason defines the set of reasons that explain why a
// particular Network Ready condition type has been raised.
type NetworkReadyConditionReason string

const (
// ParamsNotReady indicates that the resource referenced in params is not ready.
ParamsNotReady NetworkReadyConditionReason = "ParamsNotReady"
)

// ##########################
// ### GKENetworkParamSet ###

// GKENetworkParamSetConditionType is the type for status conditions on
// a GKENetworkParamSet. This type should be used with the
// GKENetworkParamSetStatus.Conditions field.
type GKENetworkParamSetConditionType string

const (
// GKENetworkParamSetStatusReady is the condition type that holds
// if the GKENetworkParamSet object is validated
GKENetworkParamSetStatusReady GKENetworkParamSetConditionType = "Ready"
)

// GKENetworkParamSetConditionReason defines the set of reasons that explain why a
// particular GKENetworkParamSet condition type has been raised.
type GKENetworkParamSetConditionReason string

const (
// SubnetNotFound indicates that the specified subnet was not found.
SubnetNotFound GKENetworkParamSetConditionReason = "SubnetNotFound"
// SecondaryRangeNotFound indicates that the specified secondary range was not found.
SecondaryRangeNotFound GKENetworkParamSetConditionReason = "SecondaryRangeNotFound"
// DeviceModeCantBeUsedWithSecondaryRange indicates that device mode was used with a secondary range.
DeviceModeCantBeUsedWithSecondaryRange GKENetworkParamSetConditionReason = "DeviceModeCantBeUsedWithSecondaryRange"
// DeviceModeVPCAlreadyInUse indicates that the VPC is already in use by another GKENetworkParamSet resource.
DeviceModeVPCAlreadyInUse GKENetworkParamSetConditionReason = "DeviceModeVPCAlreadyInUse"
// DeviceModeCantUseDefaultVPC indicates that a device mode GKENetworkParamSet cannot use the default VPC.
DeviceModeCantUseDefaultVPC GKENetworkParamSetConditionReason = "DeviceModeCantUseDefaultVPC"
// DPDKUnsupported indicates that DPDK device mode is not supported on the current cluster.
DPDKUnsupported GKENetworkParamSetConditionReason = "DPDKUnsupported"
)

// GNPNetworkParamsReadyConditionReason defines the set of reasons that explains
// the ParamsReady condition on the referencing Network resource.
type GNPNetworkParamsReadyConditionReason string

const (
// L3SecondaryMissing indicates that the L3 type Network resource is
// referencing a GKENetworkParamSet with secondary range unspecified.
L3SecondaryMissing GNPNetworkParamsReadyConditionReason = "L3SecondaryMissing"
// L3DeviceModeExists indicates that the L3 type Network resource is
// referencing a GKENetworkParamSet with device mode specified.
L3DeviceModeExists GNPNetworkParamsReadyConditionReason = "L3DeviceModeExists"
// DeviceModeMissing indicates that the Device type Network resource is
// referencing a GKENetworkParamSet with device mode unspecified.
DeviceModeMissing GNPNetworkParamsReadyConditionReason = "DeviceModeMissing"
// DeviceSecondaryExists indicates that the Device type Network resource is
// referencing a GKENetworkParamSet with a secondary range specified.
DeviceSecondaryExists GNPNetworkParamsReadyConditionReason = "DeviceSecondaryExists"
)

// ###############
// ### helpers ###

// GetCondition returns the "condType" condition from the obj. Only Network and
// GKENetworkParamSet types are supported. Returns nil when not found.
func GetCondition(obj interface{}, condType string) (*metav1.Condition, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion

create a struct that wraps metav1.Condition that implements the specific CRUD for conditions that we care about. Might be cleaner for users than one-off functions.

func NewConditionCRUD(c*metav1.Condition) ...
type ConditionCRUD struct { ... }


crud := NewConditionCRUD(obj.Status.Conditions)
if crud.Ready() { ...
}

crud.SetReady(false, "it's not ready")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I were looking into this I found https://github.com/kubernetes/apimachinery/blob/master/pkg/api/meta/conditions.go which has all the required functions, will close this PR.

var conditions []metav1.Condition

switch obj.(type) {
case *Network:
conditions = obj.(*Network).Status.Conditions
case *GKENetworkParamSet:
conditions = obj.(*GKENetworkParamSet).Status.Conditions
default:
return nil, fmt.Errorf("unsupported type: %T", obj)
}

for _, cond := range conditions {
if cond.Type == condType {
return cond.DeepCopy(), nil
}
}
return nil, nil
}

// IsReady returns true when the specified obj Ready condition is True.
// Only Network and GKENetworkParamSet types are supported.
func IsReady(obj interface{}) (bool, error) {
cond, err := GetCondition(obj, "Ready")
if err != nil {
return false, err
}
if cond != nil {
return cond.Status == metav1.ConditionTrue, nil
}
return false, nil
}
176 changes: 176 additions & 0 deletions crd/apis/network/v1/conditions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package v1

import (
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestIsReady(t *testing.T) {
makeNetwork := func(cond []metav1.Condition) *Network {
return &Network{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: NetworkStatus{
Conditions: cond,
},
}
}

tests := []struct {
name string
input interface{}
want bool
expectedErr bool
}{
{
name: "nil condition",
input: &Network{},
want: false,
},
{
name: "true condition",
input: makeNetwork([]metav1.Condition{
{
Type: string(NetworkConditionStatusParamsReady),
Status: metav1.ConditionFalse,
},
{
Type: string(NetworkConditionStatusReady),
Status: metav1.ConditionTrue,
},
}),
want: true,
},
{
name: "false condition",
input: makeNetwork([]metav1.Condition{
{
Type: string(NetworkConditionStatusReady),
Status: metav1.ConditionFalse,
},
{
Type: string(NetworkConditionStatusParamsReady),
Status: metav1.ConditionTrue,
},
}),
want: false,
},
{
name: "missing ready condition",
input: makeNetwork([]metav1.Condition{
{
Type: string(NetworkConditionStatusParamsReady),
Status: metav1.ConditionTrue,
},
}),
want: false,
},
{
name: "unsupported type",
input: metav1.APIGroup{},
want: false,
expectedErr: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := IsReady(tc.input)
if tc.expectedErr && err == nil {
t.Fatalf("IsReady(%+v) expected error but got nil", tc.input)
} else if !tc.expectedErr && err != nil {
t.Fatalf("IsReady(%+v) unexpected error %v", tc.input, err)
}
if tc.expectedErr {
return
}
if got != tc.want {
t.Fatalf("IsReady(%+v) returns %v but want %v", tc.input, got, tc.want)
}
})
}
}

func TestGetCondition(t *testing.T) {
makeNetwork := func(cond []metav1.Condition) *Network {
return &Network{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: NetworkStatus{
Conditions: cond,
},
}
}

tests := []struct {
name string
input interface{}
condType NetworkConditionType
want *metav1.Condition
expectedErr bool
}{
{
name: "nil condition",
input: &Network{},
condType: NetworkConditionStatusReady,
want: nil,
},
{
name: "condition present",
input: makeNetwork([]metav1.Condition{
{
Type: string(NetworkConditionStatusParamsReady),
Status: metav1.ConditionFalse,
},
{
Type: string(NetworkConditionStatusReady),
Status: metav1.ConditionTrue,
},
}),
condType: NetworkConditionStatusReady,
want: &metav1.Condition{
Type: string(NetworkConditionStatusReady),
Status: metav1.ConditionTrue,
},
},
{
name: "condition not present",
input: makeNetwork([]metav1.Condition{
{
Type: string(NetworkConditionStatusParamsReady),
Status: metav1.ConditionFalse,
},
}),
condType: NetworkConditionStatusReady,
want: nil,
},
{
name: "unsupported type",
input: metav1.APIGroup{},
condType: NetworkConditionStatusReady,
want: nil,
expectedErr: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := GetCondition(tc.input, string(tc.condType))
if tc.expectedErr && err == nil {
t.Fatalf("GetCondition(%+v) expected error but got nil", tc.input)
} else if !tc.expectedErr && err != nil {
t.Fatalf("GetCondition(%+v) unexpected error %v", tc.input, err)
}
if tc.expectedErr {
return
}
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf("GetCondition() has diff (-want +got):\n%s", diff)
}
})
}
}
49 changes: 0 additions & 49 deletions crd/apis/network/v1/gkenetworkparamset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,55 +64,6 @@ type NetworkRanges struct {
CIDRBlocks []string `json:"cidrBlocks"`
}

// GKENetworkParamSetConditionType is the type for status conditions on
// a GKENetworkParamSet. This type should be used with the
// GKENetworkParamSetStatus.Conditions field.
type GKENetworkParamSetConditionType string

const (
// GKENetworkParamSetStatusReady is the condition type that holds
// if the GKENetworkParamSet object is validated
GKENetworkParamSetStatusReady GKENetworkParamSetConditionType = "Ready"
)

// GKENetworkParamSetConditionReason defines the set of reasons that explain why a
// particular GKENetworkParamSet condition type has been raised.
type GKENetworkParamSetConditionReason string

const (
// SubnetNotFound indicates that the specified subnet was not found.
SubnetNotFound GKENetworkParamSetConditionReason = "SubnetNotFound"
// SecondaryRangeNotFound indicates that the specified secondary range was not found.
SecondaryRangeNotFound GKENetworkParamSetConditionReason = "SecondaryRangeNotFound"
// DeviceModeCantBeUsedWithSecondaryRange indicates that device mode was used with a secondary range.
DeviceModeCantBeUsedWithSecondaryRange GKENetworkParamSetConditionReason = "DeviceModeCantBeUsedWithSecondaryRange"
// DeviceModeVPCAlreadyInUse indicates that the VPC is already in use by another GKENetworkParamSet resource.
DeviceModeVPCAlreadyInUse GKENetworkParamSetConditionReason = "DeviceModeVPCAlreadyInUse"
// DeviceModeCantUseDefaultVPC indicates that a device mode GKENetworkParamSet cannot use the default VPC.
DeviceModeCantUseDefaultVPC GKENetworkParamSetConditionReason = "DeviceModeCantUseDefaultVPC"
// DPDKUnsupported indicates that DPDK device mode is not supported on the current cluster.
DPDKUnsupported GKENetworkParamSetConditionReason = "DPDKUnsupported"
)

// GNPNetworkParamsReadyConditionReason defines the set of reasons that explains
// the ParamsReady condition on the referencing Network resource.
type GNPNetworkParamsReadyConditionReason string

const (
// L3SecondaryMissing indicates that the L3 type Network resource is
// referencing a GKENetworkParamSet with secondary range unspecified.
L3SecondaryMissing GNPNetworkParamsReadyConditionReason = "L3SecondaryMissing"
// L3DeviceModeExists indicates that the L3 type Network resource is
// referencing a GKENetworkParamSet with device mode specified.
L3DeviceModeExists GNPNetworkParamsReadyConditionReason = "L3DeviceModeExists"
// DeviceModeMissing indicates that the Device type Network resource is
// referencing a GKENetworkParamSet with device mode unspecified.
DeviceModeMissing GNPNetworkParamsReadyConditionReason = "DeviceModeMissing"
// DeviceSecondaryExists indicates that the Device type Network resource is
// referencing a GKENetworkParamSet with a secondary range specified.
DeviceSecondaryExists GNPNetworkParamsReadyConditionReason = "DeviceSecondaryExists"
)

// GKENetworkParamSetStatus contains the status information related to the network.
type GKENetworkParamSetStatus struct {
// PodCIDRs specifies the CIDRs from which IPs will be used for Pod interfaces
Expand Down
24 changes: 0 additions & 24 deletions crd/apis/network/v1/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,6 @@ type Route struct {
To string `json:"to"`
}

// NetworkConditionType is the type for status conditions on
// a Network. This type should be used with the
// NetworkStatus.Conditions field.
type NetworkConditionType string

const (
// NetworkStatusReady is the condition type that holds
// if the Network object is validated
NetworkConditionStatusReady NetworkConditionType = "Ready"

// NetworkStatusParamsReady is the condition type that holds
// if the params object referenced by Network is validated
NetworkConditionStatusParamsReady NetworkConditionType = "ParamsReady"
)

// NetworkReadyConditionReason defines the set of reasons that explain why a
// particular Network Ready condition type has been raised.
type NetworkReadyConditionReason string

const (
// ParamsNotReady indicates that the resource referenced in params is not ready.
ParamsNotReady NetworkReadyConditionReason = "ParamsNotReady"
)

// NetworkStatus contains the status information related to the network.
type NetworkStatus struct {
// Conditions is a field representing the current conditions of the Network.
Expand Down