Skip to content

Commit

Permalink
CCC should be fetched when 'spec.mode' in CC is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
maqiuyujoyce committed Jul 16, 2024
1 parent 746449b commit beb830d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion operator/pkg/kccstate/kccstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func FetchLiveKCCState(ctx context.Context, c client.Client, resourceNN types.Na
return v1beta1.ConfigConnector{}, v1beta1.ConfigConnectorContext{}, err
}

if cc.Spec.Mode == k8s.NamespacedMode {
// Namespaced mode is the default mode for the ConfigConnector object.
if cc.Spec.Mode == "" || cc.Spec.Mode == k8s.NamespacedMode {
var ccc v1beta1.ConfigConnectorContext
if err := c.Get(ctx, types.NamespacedName{
Name: k8s.ConfigConnectorContextAllowedName,
Expand Down
53 changes: 53 additions & 0 deletions pkg/k8s/stateintospecdefaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
func TestStateIntoSpecDefaulter_ApplyDefaults(t *testing.T) {
t.Parallel()
absentValue := corev1beta1.StateIntoSpecAbsent
mergeValue := corev1beta1.StateIntoSpecMerge
tests := []struct {
name string
resource *unstructured.Unstructured
Expand Down Expand Up @@ -97,6 +98,39 @@ func TestStateIntoSpecDefaulter_ApplyDefaults(t *testing.T) {
Name: operatork8s.ConfigConnectorAllowedName,
},
Spec: corev1beta1.ConfigConnectorSpec{
Mode: "cluster",
StateIntoSpec: &absentValue,
},
},
expectChanged: true,
expectValue: "absent",
},
{
name: "use ccc default value when mode is unset in cc",
resource: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "pubsub.cnrm.cloud.google.com/v1beta1",
"kind": "PubSubTopic",
"metadata": map[string]interface{}{
"name": "test-name",
"namespace": "test-ns",
},
},
},
cc: &corev1beta1.ConfigConnector{
ObjectMeta: metav1.ObjectMeta{
Name: operatork8s.ConfigConnectorAllowedName,
},
Spec: corev1beta1.ConfigConnectorSpec{
StateIntoSpec: &mergeValue,
},
},
ccc: &corev1beta1.ConfigConnectorContext{
ObjectMeta: metav1.ObjectMeta{
Name: operatork8s.ConfigConnectorContextAllowedName,
Namespace: "test-ns",
},
Spec: corev1beta1.ConfigConnectorContextSpec{
StateIntoSpec: &absentValue,
},
},
Expand Down Expand Up @@ -135,6 +169,25 @@ func TestStateIntoSpecDefaulter_ApplyDefaults(t *testing.T) {
expectChanged: true,
expectValue: "absent",
},
{
name: "error due to ccc not found when mode is unset in cc",
resource: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "pubsub.cnrm.cloud.google.com/v1beta1",
"kind": "PubSubTopic",
"metadata": map[string]interface{}{
"name": "test-name",
"namespace": "test-ns",
},
},
},
cc: &corev1beta1.ConfigConnector{
ObjectMeta: metav1.ObjectMeta{
Name: operatork8s.ConfigConnectorAllowedName,
},
},
expectError: true,
},
{
name: "error due to ccc not found",
resource: &unstructured.Unstructured{
Expand Down

0 comments on commit beb830d

Please sign in to comment.