Skip to content

Commit 61af7cb

Browse files
authored
Merge pull request agones-dev#108 from googleprivate/refactor/crd-established
Move WaitForEstablishedCRD into central `crd` package
2 parents ed34a5f + af9da96 commit 61af7cb

4 files changed

Lines changed: 127 additions & 69 deletions

File tree

pkg/gameservers/controller.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"net/http"
21-
"time"
2221

2322
"agones.dev/agones/pkg/apis/stable"
2423
stablev1alpha1 "agones.dev/agones/pkg/apis/stable/v1alpha1"
2524
"agones.dev/agones/pkg/client/clientset/versioned"
2625
getterv1alpha1 "agones.dev/agones/pkg/client/clientset/versioned/typed/stable/v1alpha1"
2726
"agones.dev/agones/pkg/client/informers/externalversions"
2827
listerv1alpha1 "agones.dev/agones/pkg/client/listers/stable/v1alpha1"
28+
"agones.dev/agones/pkg/util/crd"
2929
"agones.dev/agones/pkg/util/runtime"
3030
"agones.dev/agones/pkg/util/webhooks"
3131
"agones.dev/agones/pkg/util/workerqueue"
@@ -34,14 +34,12 @@ import (
3434
"github.com/sirupsen/logrus"
3535
admv1beta1 "k8s.io/api/admission/v1beta1"
3636
corev1 "k8s.io/api/core/v1"
37-
apiv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
3837
extclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3938
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
4039
k8serrors "k8s.io/apimachinery/pkg/api/errors"
4140
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4241
"k8s.io/apimachinery/pkg/labels"
4342
"k8s.io/apimachinery/pkg/util/intstr"
44-
"k8s.io/apimachinery/pkg/util/wait"
4543
"k8s.io/client-go/informers"
4644
"k8s.io/client-go/kubernetes"
4745
"k8s.io/client-go/kubernetes/scheme"
@@ -231,7 +229,7 @@ func (c *Controller) Run(threadiness int, stop <-chan struct{}) error {
231229
}()
232230
defer c.server.Close() // nolint: errcheck
233231

234-
err := c.waitForEstablishedCRD()
232+
err := crd.WaitForEstablishedCRD(c.crdGetter, "gameservers.stable.agones.dev", c.logger)
235233
if err != nil {
236234
return err
237235
}
@@ -606,26 +604,3 @@ func (c *Controller) address(pod *corev1.Pod) (string, error) {
606604

607605
return "", errors.Errorf("Could not find an address for Node: %s", node.ObjectMeta.Name)
608606
}
609-
610-
// waitForEstablishedCRD blocks until CRD comes to an Established state.
611-
// Has a deadline of 60 seconds for this to occur.
612-
func (c *Controller) waitForEstablishedCRD() error {
613-
return wait.PollImmediate(500*time.Millisecond, 60*time.Second, func() (done bool, err error) {
614-
crd, err := c.crdGetter.Get("gameservers.stable.agones.dev", metav1.GetOptions{})
615-
if err != nil {
616-
return false, err
617-
}
618-
619-
for _, cond := range crd.Status.Conditions {
620-
switch cond.Type {
621-
case apiv1beta1.Established:
622-
if cond.Status == apiv1beta1.ConditionTrue {
623-
c.logger.WithField("crd", crd).Info("GameServer custom resource definition is established")
624-
return true, err
625-
}
626-
}
627-
}
628-
629-
return false, nil
630-
})
631-
}

pkg/gameservers/controller_test.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"net/http"
21-
"sync"
2221
"testing"
2322
"time"
2423

@@ -42,47 +41,6 @@ import (
4241
"k8s.io/client-go/tools/cache"
4342
)
4443

45-
func TestControllerWaitForEstablishedCRD(t *testing.T) {
46-
t.Parallel()
47-
crd := newEstablishedCRD()
48-
t.Run("CRD already established", func(t *testing.T) {
49-
con, mocks := newFakeController()
50-
mocks.extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
51-
return true, crd, nil
52-
})
53-
54-
err := con.waitForEstablishedCRD()
55-
assert.Nil(t, err)
56-
})
57-
58-
t.Run("CRD takes a second to become established", func(t *testing.T) {
59-
t.Parallel()
60-
con, mocks := newFakeController()
61-
62-
m := sync.RWMutex{}
63-
established := false
64-
65-
mocks.extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
66-
m.RLock()
67-
defer m.RUnlock()
68-
if established {
69-
return true, crd, nil
70-
}
71-
return false, nil, nil
72-
})
73-
74-
go func() {
75-
time.Sleep(3 * time.Second)
76-
m.Lock()
77-
defer m.Unlock()
78-
established = true
79-
}()
80-
81-
err := con.waitForEstablishedCRD()
82-
assert.Nil(t, err)
83-
})
84-
}
85-
8644
func TestControllerSyncGameServer(t *testing.T) {
8745
t.Parallel()
8846

pkg/util/crd/crd.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// package crd contains utilities for working with
16+
// CustomResourceDefinitions
17+
package crd
18+
19+
import (
20+
"time"
21+
22+
"github.com/sirupsen/logrus"
23+
apiv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
24+
extv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/util/wait"
27+
)
28+
29+
// WaitForEstablishedCRD blocks until CRD comes to an Established state.
30+
// Has a deadline of 60 seconds for this to occur.
31+
func WaitForEstablishedCRD(crdGetter extv1beta1.CustomResourceDefinitionInterface, name string, logger *logrus.Entry) error {
32+
return wait.PollImmediate(time.Second, 60*time.Second, func() (done bool, err error) {
33+
crd, err := crdGetter.Get(name, v1.GetOptions{})
34+
if err != nil {
35+
return false, err
36+
}
37+
38+
for _, cond := range crd.Status.Conditions {
39+
switch cond.Type {
40+
case apiv1beta1.Established:
41+
if cond.Status == apiv1beta1.ConditionTrue {
42+
logger.WithField("crd", crd.ObjectMeta.Name).Info("custom resource definition established")
43+
return true, err
44+
}
45+
}
46+
}
47+
48+
return false, nil
49+
})
50+
}

pkg/util/crd/crd_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2018 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package crd
16+
17+
import (
18+
"sync"
19+
"testing"
20+
"time"
21+
22+
"github.com/sirupsen/logrus"
23+
"github.com/stretchr/testify/assert"
24+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
25+
extfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
k8stesting "k8s.io/client-go/testing"
28+
)
29+
30+
func TestWaitForEstablishedCRD(t *testing.T) {
31+
t.Parallel()
32+
crd := &v1beta1.CustomResourceDefinition{
33+
Status: v1beta1.CustomResourceDefinitionStatus{
34+
Conditions: []v1beta1.CustomResourceDefinitionCondition{{
35+
Type: v1beta1.Established,
36+
Status: v1beta1.ConditionTrue,
37+
}},
38+
},
39+
}
40+
41+
t.Run("CRD already established", func(t *testing.T) {
42+
extClient := &extfake.Clientset{}
43+
extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
44+
return true, crd, nil
45+
})
46+
47+
err := WaitForEstablishedCRD(extClient.ApiextensionsV1beta1().CustomResourceDefinitions(), "test", logrus.WithField("test", "already-established"))
48+
assert.Nil(t, err)
49+
})
50+
51+
t.Run("CRD takes a second to become established", func(t *testing.T) {
52+
extClient := &extfake.Clientset{}
53+
m := sync.RWMutex{}
54+
established := false
55+
56+
extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
57+
m.RLock()
58+
defer m.RUnlock()
59+
if established {
60+
return true, crd, nil
61+
}
62+
return false, nil, nil
63+
})
64+
65+
go func() {
66+
time.Sleep(3 * time.Second)
67+
m.Lock()
68+
defer m.Unlock()
69+
established = true
70+
}()
71+
72+
err := WaitForEstablishedCRD(extClient.ApiextensionsV1beta1().CustomResourceDefinitions(), "test", logrus.WithField("test", "already-established"))
73+
assert.Nil(t, err)
74+
})
75+
}

0 commit comments

Comments
 (0)