Merge pull request #3267 from zackzhangkai/fix-controller-manager

fix application reconcile
This commit is contained in:
KubeSphere CI Bot
2021-01-21 15:57:40 +08:00
committed by GitHub
10 changed files with 151 additions and 203 deletions

View File

@@ -50,14 +50,14 @@ func (d *applicationsGetter) Get(namespace, name string) (runtime.Object, error)
func (d *applicationsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
applications := appv1beta1.ApplicationList{}
err := d.c.List(context.Background(), &applications, &client.ListOptions{Namespace: namespace})
err := d.c.List(context.Background(), &applications, &client.ListOptions{Namespace: namespace, LabelSelector: query.Selector()})
if err != nil {
klog.Error(err)
return nil, err
}
var result []runtime.Object
for _, app := range applications.Items {
result = append(result, &app)
for i := range applications.Items {
result = append(result, &applications.Items[i])
}
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil

View File

@@ -24,11 +24,11 @@ import (
"k8s.io/klog/v2"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"path/filepath"
"reflect"
appv1beta1 "sigs.k8s.io/application/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"testing"
)
@@ -46,6 +46,15 @@ func createNamespace(name string, ctx context.Context) {
}
}
func compare(actual *appv1beta1.Application, expects ...*appv1beta1.Application) bool {
for _, app := range expects {
if actual.Name == app.Name && actual.Namespace == app.Namespace && reflect.DeepEqual(actual.Labels, app.Labels) {
return true
}
}
return false
}
func TestGetListApplications(t *testing.T) {
e := &envtest.Environment{CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "..", "config", "crds")}}
cfg, err := e.Start()
@@ -66,29 +75,68 @@ func TestGetListApplications(t *testing.T) {
c, _ = client.New(cfg, client.Options{Scheme: sch})
var labelSet1 = map[string]string{"foo": "bar"}
application := &appv1beta1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "foo",
Labels: labelSet1,
var labelSet1 = map[string]string{"foo-1": "bar-1"}
var labelSet2 = map[string]string{"foo-2": "bar-2"}
var ns = "ns-1"
testCases := []*appv1beta1.Application{
{
ObjectMeta: metav1.ObjectMeta{
Name: "app-1",
Namespace: ns,
Labels: labelSet1,
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "app-2",
Namespace: ns,
Labels: labelSet2,
},
},
}
ctx := context.TODO()
createNamespace("foo", ctx)
_ = c.Create(ctx, application)
createNamespace(ns, ctx)
for _, app := range testCases {
if err = c.Create(ctx, app); err != nil {
t.Fatal(err)
}
}
getter := New(ce)
_, err = getter.List("foo", &query.Query{})
results, err := getter.List(ns, &query.Query{})
if err != nil {
t.Fatal(err)
}
_, err = getter.Get("foo", "bar")
if results.TotalItems != len(testCases) {
t.Fatal("TotalItems is not match")
}
if len(results.Items) != len(testCases) {
t.Fatal("Items numbers is not match mock data")
}
for _, app := range results.Items {
app, err := app.(*appv1beta1.Application)
if !err {
t.Fatal(err)
}
if !compare(app, testCases...) {
t.Errorf("The results %v not match testcases %v", results.Items, testCases)
}
}
result, err := getter.Get(ns, "app-1")
if err != nil {
t.Fatal(err)
}
app := result.(*appv1beta1.Application)
if !compare(app, testCases...) {
t.Errorf("The results %v not match testcases %v", result, testCases)
}
}

View File

@@ -1,85 +0,0 @@
/*
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.
*/
package servicemesh
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
const (
AppLabel = "app"
VersionLabel = "version"
ApplicationNameLabel = "app.kubernetes.io/name"
ApplicationVersionLabel = "app.kubernetes.io/version"
)
var ApplicationLabels = [...]string{
ApplicationNameLabel,
ApplicationVersionLabel,
AppLabel,
}
var TrimChars = [...]string{".", "_", "-"}
// normalize version names
// strip [_.-]
func NormalizeVersionName(version string) string {
for _, char := range TrimChars {
version = strings.ReplaceAll(version, char, "")
}
return version
}
func GetComponentName(meta *metav1.ObjectMeta) string {
if len(meta.Labels[AppLabel]) > 0 {
return meta.Labels[AppLabel]
}
return ""
}
func GetComponentVersion(meta *metav1.ObjectMeta) string {
if len(meta.Labels[VersionLabel]) > 0 {
return meta.Labels[VersionLabel]
}
return ""
}
func ExtractApplicationLabels(meta *metav1.ObjectMeta) map[string]string {
labels := make(map[string]string, 0)
for _, label := range ApplicationLabels {
if len(meta.Labels[label]) == 0 {
return nil
} else {
labels[label] = meta.Labels[label]
}
}
return labels
}
func IsApplicationComponent(meta *metav1.ObjectMeta) bool {
for _, label := range ApplicationLabels {
if len(meta.Labels[label]) == 0 {
return false
}
}
return true
}

View File

@@ -1,23 +0,0 @@
/*
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.
*/
package metrics
import "github.com/emicklei/go-restful"
func GetAppMetrics(request *restful.Request, response *restful.Response) {
}