mirror of
https://github.com/kubernetes/sample-controller.git
synced 2025-02-01 01:12:52 +08:00
upgrade to apps/v1 deployment
Kubernetes-commit: 386e001446b8e9c96daf15d71d3176a73d638b50
This commit is contained in:
parent
4a1ec65955
commit
994cb3621c
@ -21,7 +21,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
appsv1beta2 "k8s.io/api/apps/v1beta2"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -32,7 +32,7 @@ import (
|
|||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
"k8s.io/client-go/kubernetes/scheme"
|
||||||
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
appslisters "k8s.io/client-go/listers/apps/v1beta2"
|
appslisters "k8s.io/client-go/listers/apps/v1"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
"k8s.io/client-go/tools/record"
|
"k8s.io/client-go/tools/record"
|
||||||
"k8s.io/client-go/util/workqueue"
|
"k8s.io/client-go/util/workqueue"
|
||||||
@ -93,7 +93,7 @@ func NewController(
|
|||||||
|
|
||||||
// obtain references to shared index informers for the Deployment and Foo
|
// obtain references to shared index informers for the Deployment and Foo
|
||||||
// types.
|
// types.
|
||||||
deploymentInformer := kubeInformerFactory.Apps().V1beta2().Deployments()
|
deploymentInformer := kubeInformerFactory.Apps().V1().Deployments()
|
||||||
fooInformer := sampleInformerFactory.Samplecontroller().V1alpha1().Foos()
|
fooInformer := sampleInformerFactory.Samplecontroller().V1alpha1().Foos()
|
||||||
|
|
||||||
// Create event broadcaster
|
// Create event broadcaster
|
||||||
@ -134,8 +134,8 @@ func NewController(
|
|||||||
deploymentInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
deploymentInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||||
AddFunc: controller.handleObject,
|
AddFunc: controller.handleObject,
|
||||||
UpdateFunc: func(old, new interface{}) {
|
UpdateFunc: func(old, new interface{}) {
|
||||||
newDepl := new.(*appsv1beta2.Deployment)
|
newDepl := new.(*appsv1.Deployment)
|
||||||
oldDepl := old.(*appsv1beta2.Deployment)
|
oldDepl := old.(*appsv1.Deployment)
|
||||||
if newDepl.ResourceVersion == oldDepl.ResourceVersion {
|
if newDepl.ResourceVersion == oldDepl.ResourceVersion {
|
||||||
// Periodic resync will send update events for all known Deployments.
|
// Periodic resync will send update events for all known Deployments.
|
||||||
// Two different versions of the same Deployment will always have different RVs.
|
// Two different versions of the same Deployment will always have different RVs.
|
||||||
@ -277,7 +277,7 @@ func (c *Controller) syncHandler(key string) error {
|
|||||||
deployment, err := c.deploymentsLister.Deployments(foo.Namespace).Get(deploymentName)
|
deployment, err := c.deploymentsLister.Deployments(foo.Namespace).Get(deploymentName)
|
||||||
// If the resource doesn't exist, we'll create it
|
// If the resource doesn't exist, we'll create it
|
||||||
if errors.IsNotFound(err) {
|
if errors.IsNotFound(err) {
|
||||||
deployment, err = c.kubeclientset.AppsV1beta2().Deployments(foo.Namespace).Create(newDeployment(foo))
|
deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Create(newDeployment(foo))
|
||||||
}
|
}
|
||||||
|
|
||||||
// If an error occurs during Get/Create, we'll requeue the item so we can
|
// If an error occurs during Get/Create, we'll requeue the item so we can
|
||||||
@ -299,8 +299,8 @@ func (c *Controller) syncHandler(key string) error {
|
|||||||
// number does not equal the current desired replicas on the Deployment, we
|
// number does not equal the current desired replicas on the Deployment, we
|
||||||
// should update the Deployment resource.
|
// should update the Deployment resource.
|
||||||
if foo.Spec.Replicas != nil && *foo.Spec.Replicas != *deployment.Spec.Replicas {
|
if foo.Spec.Replicas != nil && *foo.Spec.Replicas != *deployment.Spec.Replicas {
|
||||||
glog.V(4).Infof("Foor: %d, deplR: %d", *foo.Spec.Replicas, *deployment.Spec.Replicas)
|
glog.V(4).Infof("Foo %s replicas: %d, deployment replicas: %d", name, *foo.Spec.Replicas, *deployment.Spec.Replicas)
|
||||||
deployment, err = c.kubeclientset.AppsV1beta2().Deployments(foo.Namespace).Update(newDeployment(foo))
|
deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Update(newDeployment(foo))
|
||||||
}
|
}
|
||||||
|
|
||||||
// If an error occurs during Update, we'll requeue the item so we can
|
// If an error occurs during Update, we'll requeue the item so we can
|
||||||
@ -321,7 +321,7 @@ func (c *Controller) syncHandler(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) updateFooStatus(foo *samplev1alpha1.Foo, deployment *appsv1beta2.Deployment) error {
|
func (c *Controller) updateFooStatus(foo *samplev1alpha1.Foo, deployment *appsv1.Deployment) error {
|
||||||
// NEVER modify objects from the store. It's a read-only, local cache.
|
// NEVER modify objects from the store. It's a read-only, local cache.
|
||||||
// You can use DeepCopy() to make a deep copy of original object and modify this copy
|
// You can use DeepCopy() to make a deep copy of original object and modify this copy
|
||||||
// Or create a copy manually for better performance
|
// Or create a copy manually for better performance
|
||||||
@ -391,12 +391,12 @@ func (c *Controller) handleObject(obj interface{}) {
|
|||||||
// newDeployment creates a new Deployment for a Foo resource. It also sets
|
// newDeployment creates a new Deployment for a Foo resource. It also sets
|
||||||
// the appropriate OwnerReferences on the resource so handleObject can discover
|
// the appropriate OwnerReferences on the resource so handleObject can discover
|
||||||
// the Foo resource that 'owns' it.
|
// the Foo resource that 'owns' it.
|
||||||
func newDeployment(foo *samplev1alpha1.Foo) *appsv1beta2.Deployment {
|
func newDeployment(foo *samplev1alpha1.Foo) *appsv1.Deployment {
|
||||||
labels := map[string]string{
|
labels := map[string]string{
|
||||||
"app": "nginx",
|
"app": "nginx",
|
||||||
"controller": foo.Name,
|
"controller": foo.Name,
|
||||||
}
|
}
|
||||||
return &appsv1beta2.Deployment{
|
return &appsv1.Deployment{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: foo.Spec.DeploymentName,
|
Name: foo.Spec.DeploymentName,
|
||||||
Namespace: foo.Namespace,
|
Namespace: foo.Namespace,
|
||||||
@ -408,7 +408,7 @@ func newDeployment(foo *samplev1alpha1.Foo) *appsv1beta2.Deployment {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Spec: appsv1beta2.DeploymentSpec{
|
Spec: appsv1.DeploymentSpec{
|
||||||
Replicas: foo.Spec.Replicas,
|
Replicas: foo.Spec.Replicas,
|
||||||
Selector: &metav1.LabelSelector{
|
Selector: &metav1.LabelSelector{
|
||||||
MatchLabels: labels,
|
MatchLabels: labels,
|
||||||
|
Loading…
Reference in New Issue
Block a user