-
Notifications
You must be signed in to change notification settings - Fork 218
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.