feat: kubesphere 4.0 (#6115)
* feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> * feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> --------- Signed-off-by: ci-bot <ci-bot@kubesphere.io> Co-authored-by: ks-ci-bot <ks-ci-bot@example.com> Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
committed by
GitHub
parent
b5015ec7b9
commit
447a51f08b
@@ -1,110 +1,84 @@
|
||||
/*
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
* Please refer to the LICENSE file in the root directory of the project.
|
||||
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package revisions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/client-go/informers"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
type RevisionGetter interface {
|
||||
GetDeploymentRevision(namespace, name, revision string) (*v1.ReplicaSet, error)
|
||||
GetStatefulSetRevision(namespace, name string, revision int) (*v1.ControllerRevision, error)
|
||||
GetDaemonSetRevision(namespace, name string, revision int) (*v1.ControllerRevision, error)
|
||||
GetDeploymentRevision(namespace, name, revision string) (*appsv1.ReplicaSet, error)
|
||||
GetStatefulSetRevision(namespace, name string, revision int) (*appsv1.ControllerRevision, error)
|
||||
GetDaemonSetRevision(namespace, name string, revision int) (*appsv1.ControllerRevision, error)
|
||||
}
|
||||
|
||||
type revisionGetter struct {
|
||||
informers informers.SharedInformerFactory
|
||||
cache runtimeclient.Reader
|
||||
}
|
||||
|
||||
func NewRevisionGetter(informers informers.SharedInformerFactory) RevisionGetter {
|
||||
return &revisionGetter{informers: informers}
|
||||
func NewRevisionGetter(cacheReader runtimeclient.Reader) RevisionGetter {
|
||||
return &revisionGetter{cache: cacheReader}
|
||||
}
|
||||
|
||||
func (c *revisionGetter) GetDeploymentRevision(namespace, name, revision string) (*v1.ReplicaSet, error) {
|
||||
deploymentLister := c.informers.Apps().V1().Deployments().Lister()
|
||||
deploy, err := deploymentLister.Deployments(namespace).Get(name)
|
||||
if err != nil {
|
||||
func (c *revisionGetter) GetDeploymentRevision(namespace, name, revision string) (*appsv1.ReplicaSet, error) {
|
||||
deployment := &appsv1.Deployment{}
|
||||
if err := c.cache.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: name}, deployment); err != nil {
|
||||
klog.Errorf("get deployment %s failed, reason: %s", name, err)
|
||||
return nil, err
|
||||
}
|
||||
replicaSetList := &appsv1.ReplicaSetList{}
|
||||
if err := c.cache.List(context.Background(), replicaSetList, client.InNamespace(namespace), client.MatchingLabels(deployment.Spec.Template.Labels)); err != nil {
|
||||
klog.Errorf("get deployment %s failed, reason: %s", name, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
labelMap := deploy.Spec.Template.Labels
|
||||
labelSelector := labels.Set(labelMap).AsSelector()
|
||||
|
||||
replicaSetLister := c.informers.Apps().V1().ReplicaSets().Lister()
|
||||
rsList, err := replicaSetLister.ReplicaSets(namespace).List(labelSelector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, rs := range rsList {
|
||||
if rs.Annotations["deployment.kubernetes.io/revision"] == revision {
|
||||
return rs, nil
|
||||
for _, rs := range replicaSetList.Items {
|
||||
result := rs.DeepCopy()
|
||||
if result.Annotations["deployment.kubernetes.io/revision"] == revision {
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("revision not found %v#%v", name, revision)
|
||||
}
|
||||
|
||||
func (c *revisionGetter) GetDaemonSetRevision(namespace, name string, revisionInt int) (*v1.ControllerRevision, error) {
|
||||
daemonSetLister := c.informers.Apps().V1().DaemonSets().Lister()
|
||||
ds, err := daemonSetLister.DaemonSets(namespace).Get(name)
|
||||
|
||||
if err != nil {
|
||||
func (c *revisionGetter) GetDaemonSetRevision(namespace, name string, revision int) (*appsv1.ControllerRevision, error) {
|
||||
daemonSet := &appsv1.DaemonSet{}
|
||||
if err := c.cache.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: name}, daemonSet); err != nil {
|
||||
klog.Errorf("get deployment %s failed, reason: %s", name, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lbs := ds.Spec.Template.Labels
|
||||
|
||||
return c.getControllerRevision(namespace, name, lbs, revisionInt)
|
||||
return c.getControllerRevision(namespace, name, daemonSet.Spec.Template.Labels, revision)
|
||||
}
|
||||
|
||||
func (c *revisionGetter) GetStatefulSetRevision(namespace, name string, revisionInt int) (*v1.ControllerRevision, error) {
|
||||
statefulSetLister := c.informers.Apps().V1().StatefulSets().Lister()
|
||||
st, err := statefulSetLister.StatefulSets(namespace).Get(name)
|
||||
|
||||
if err != nil {
|
||||
func (c *revisionGetter) GetStatefulSetRevision(namespace, name string, revisionInt int) (*appsv1.ControllerRevision, error) {
|
||||
statefulSet := &appsv1.StatefulSet{}
|
||||
if err := c.cache.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: name}, statefulSet); err != nil {
|
||||
klog.Errorf("get deployment %s failed, reason: %s", name, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.getControllerRevision(namespace, name, st.Spec.Template.Labels, revisionInt)
|
||||
return c.getControllerRevision(namespace, name, statefulSet.Spec.Template.Labels, revisionInt)
|
||||
}
|
||||
|
||||
func (c *revisionGetter) getControllerRevision(namespace, name string, labelMap map[string]string, revision int) (*v1.ControllerRevision, error) {
|
||||
|
||||
labelSelector := labels.Set(labelMap).AsSelector()
|
||||
controllerRevisionLister := c.informers.Apps().V1().ControllerRevisions().Lister()
|
||||
revisions, err := controllerRevisionLister.ControllerRevisions(namespace).List(labelSelector)
|
||||
|
||||
if err != nil {
|
||||
func (c *revisionGetter) getControllerRevision(namespace, name string, labelMap map[string]string, revision int) (*appsv1.ControllerRevision, error) {
|
||||
controllerRevisionList := &appsv1.ControllerRevisionList{}
|
||||
if err := c.cache.List(context.Background(), controllerRevisionList, client.InNamespace(namespace), client.MatchingLabels(labelMap)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, controllerRevision := range revisions {
|
||||
for _, controllerRevision := range controllerRevisionList.Items {
|
||||
if controllerRevision.Revision == int64(revision) {
|
||||
return controllerRevision, nil
|
||||
return controllerRevision.DeepCopy(), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("revision not found %v#%v", name, revision)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user