Skip to content

Commit 32185e6

Browse files
committedMay 18, 2018
Add namespaces to the cluster CRD.
1 parent be8e23b commit 32185e6

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed
 

‎examples/cluster-two.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ kind: Cluster
22
apiVersion: clusterregistry.k8s.io/v1alpha1
33
metadata:
44
name: also-my-cluster
5+
namespace: default
56
labels:
67
foo: bar
78
spec:

‎examples/cluster.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
kind: Cluster
22
apiVersion: clusterregistry.k8s.io/v1alpha1
33
metadata:
4+
namespace: default
45
name: my-cluster
56
labels:
67
foo: bar

‎examples/slackcontroller/slackcontroller.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,28 @@ func (c *Controller) processNextWorkItem() bool {
209209

210210
// syncHandler sends Slack notifications when the cluster is updated.
211211
func (c *Controller) syncHandler(key string) error {
212+
glog.Infof(key)
212213
// Convert the namespace/name string into a distinct namespace and name
213-
_, name, err := cache.SplitMetaNamespaceKey(key)
214+
namespace, name, err := cache.SplitMetaNamespaceKey(key)
214215
if err != nil {
215216
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
216217
return nil
217218
}
218219

219-
bodyFormatString := "Cluster %s was added."
220+
bodyFormatString := "Cluster %s was added in namespace %s."
220221
// Get the Cluster resource with this name
221-
cluster, err := c.clusterLister.Get(name)
222+
cluster, err := c.clusterLister.Clusters(namespace).Get(name)
222223
if err != nil {
223224
// The Cluster resource may have been deleted, in which case we
224225
// post a removal message.
225226
if errors.IsNotFound(err) {
226-
bodyFormatString = "Cluster %s was removed."
227+
bodyFormatString = "Cluster %s was removed from namespace %s."
227228
} else {
228229
return err
229230
}
230231
}
231232

232-
body := strings.NewReader(fmt.Sprintf("{ 'text':'%s'}", fmt.Sprintf(bodyFormatString, name)))
233+
body := strings.NewReader(fmt.Sprintf("{ 'text':'%s'}", fmt.Sprintf(bodyFormatString, name, namespace)))
233234
client := &http.Client{}
234235
resp, err := client.Post(c.slackURL, "application/json", body)
235236
if err != nil {

‎pkg/apis/clusterregistry/v1alpha1/types.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
)
2222

2323
// +genclient
24-
// +genclient:nonNamespaced
2524
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
2625

2726
// Cluster contains information about a cluster in a cluster registry.

‎test/integration/server_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
crclientset "k8s.io/cluster-registry/pkg/client/clientset/versioned"
2727
)
2828

29+
const testNamepace = "default"
30+
2931
func TestClusterCRUD(t *testing.T) {
3032
testenv := &test.TestEnvironment{CRDs: []*v1beta1.CustomResourceDefinition{&v1alpha1.ClusterCRD}}
3133

@@ -60,9 +62,10 @@ func TestClusterCRUD(t *testing.T) {
6062
}
6163

6264
func testClusterCreate(t *testing.T, clientset *crclientset.Clientset, clusterName string) {
63-
cluster, err := clientset.ClusterregistryV1alpha1().Clusters().Create(&v1alpha1.Cluster{
65+
cluster, err := clientset.ClusterregistryV1alpha1().Clusters(testNamepace).Create(&v1alpha1.Cluster{
6466
ObjectMeta: metav1.ObjectMeta{
65-
Name: clusterName,
67+
Name: clusterName,
68+
Namespace: testNamepace,
6669
},
6770
})
6871

@@ -76,7 +79,7 @@ func testClusterCreate(t *testing.T, clientset *crclientset.Clientset, clusterNa
7679
}
7780

7881
func testClusterGet(t *testing.T, clientset *crclientset.Clientset, clusterName string) {
79-
cluster, err := clientset.ClusterregistryV1alpha1().Clusters().Get(clusterName,
82+
cluster, err := clientset.ClusterregistryV1alpha1().Clusters(testNamepace).Get(clusterName,
8083
metav1.GetOptions{})
8184

8285
if err != nil {
@@ -89,7 +92,7 @@ func testClusterGet(t *testing.T, clientset *crclientset.Clientset, clusterName
8992
}
9093

9194
func testClusterUpdate(t *testing.T, clientset *crclientset.Clientset, clusterName string) {
92-
cluster, err := clientset.ClusterregistryV1alpha1().Clusters().Get(clusterName, metav1.GetOptions{})
95+
cluster, err := clientset.ClusterregistryV1alpha1().Clusters(testNamepace).Get(clusterName, metav1.GetOptions{})
9396
if err != nil {
9497
t.Fatalf("Unexpected error: %v", err)
9598
}
@@ -102,7 +105,7 @@ func testClusterUpdate(t *testing.T, clientset *crclientset.Clientset, clusterNa
102105
},
103106
}
104107

105-
cluster, err = clientset.ClusterregistryV1alpha1().Clusters().Update(cluster)
108+
cluster, err = clientset.ClusterregistryV1alpha1().Clusters(testNamepace).Update(cluster)
106109

107110
if err != nil {
108111
t.Fatalf("Unexpected error: %v", err)
@@ -117,15 +120,15 @@ func testClusterUpdate(t *testing.T, clientset *crclientset.Clientset, clusterNa
117120
}
118121

119122
func testClusterDelete(t *testing.T, clientset *crclientset.Clientset, clusterName string) {
120-
err := clientset.ClusterregistryV1alpha1().Clusters().Delete(clusterName,
123+
err := clientset.ClusterregistryV1alpha1().Clusters(testNamepace).Delete(clusterName,
121124
&metav1.DeleteOptions{})
122125

123126
if err != nil {
124127
t.Fatalf("Unexpected error: %v", err)
125128
}
126129

127130
// We do not expect to find the cluster we just deleted
128-
_, err = clientset.ClusterregistryV1alpha1().Clusters().Get(clusterName, metav1.GetOptions{})
131+
_, err = clientset.ClusterregistryV1alpha1().Clusters(testNamepace).Get(clusterName, metav1.GetOptions{})
129132

130133
if err == nil {
131134
t.Fatalf("Unexpected error: %v", err)

0 commit comments

Comments
 (0)
Please sign in to comment.