Skip to content

Commit

Permalink
remove kube-aggregator/pkg/client/*
Browse files Browse the repository at this point in the history
use v1 api

[DO NOT REVIEW] bazel changes

code-gen script changes

update hack/update-vendor

remove useless interface type cast

Kubernetes-commit: 3b1624f0cc5c1db9ea1093ca167a0250059b6a62
  • Loading branch information
yue9944882 authored and k8s-publishing-bot committed Jun 26, 2019
1 parent f7e4e3a commit 8a45099
Show file tree
Hide file tree
Showing 40 changed files with 534 additions and 1,593 deletions.
2 changes: 1 addition & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bash "${CODEGEN_PKG}/generate-groups.sh" deepcopy,client,lister,informer \
CLIENTSET_NAME_VERSIONED=clientset \
CLIENTSET_PKG_NAME=clientset_generated \
CLIENTSET_NAME_INTERNAL=internalclientset \
bash "${CODEGEN_PKG}/generate-internal-groups.sh" deepcopy,client,lister,informer,conversion \
bash "${CODEGEN_PKG}/generate-internal-groups.sh" deepcopy,conversion \
k8s.io/kube-aggregator/pkg/client k8s.io/kube-aggregator/pkg/apis k8s.io/kube-aggregator/pkg/apis \
"apiregistration:v1beta1,v1" \
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
// force compilation of packages we'll later rely upon
_ "k8s.io/kube-aggregator/pkg/apis/apiregistration/install"
_ "k8s.io/kube-aggregator/pkg/apis/apiregistration/validation"
_ "k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset"
_ "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion"
_ "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
_ "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/v1"
_ "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/v1beta1"
)

Expand Down
9 changes: 0 additions & 9 deletions pkg/apis/apiregistration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ package apiregistration

import (
"sort"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
)

Expand Down Expand Up @@ -84,13 +82,6 @@ func (s ByVersionPriority) Less(i, j int) bool {
return version.CompareKubeAwareVersionStrings(s[i].Spec.Version, s[j].Spec.Version) > 0
}

// APIServiceNameToGroupVersion returns the GroupVersion for a given apiServiceName. The name
// must be valid, but any object you get back from an informer will be valid.
func APIServiceNameToGroupVersion(apiServiceName string) schema.GroupVersion {
tokens := strings.SplitN(apiServiceName, ".", 2)
return schema.GroupVersion{Group: tokens[1], Version: tokens[0]}
}

// NewLocalAvailableAPIServiceCondition returns a condition for an available local APIService.
func NewLocalAvailableAPIServiceCondition() APIServiceCondition {
return APIServiceCondition{
Expand Down
138 changes: 138 additions & 0 deletions pkg/apis/apiregistration/v1/helper/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
Copyright 2016 The Kubernetes 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 helper

import (
"sort"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
)

// SortedByGroupAndVersion sorts APIServices into their different groups, and then sorts them based on their versions.
// For example, the first element of the first array contains the APIService with the highest version number, in the
// group with the highest priority; while the last element of the last array contains the APIService with the lowest
// version number, in the group with the lowest priority.
func SortedByGroupAndVersion(servers []*v1.APIService) [][]*v1.APIService {
serversByGroupPriorityMinimum := ByGroupPriorityMinimum(servers)
sort.Sort(serversByGroupPriorityMinimum)

ret := [][]*v1.APIService{}
for _, curr := range serversByGroupPriorityMinimum {
// check to see if we already have an entry for this group
existingIndex := -1
for j, groupInReturn := range ret {
if groupInReturn[0].Spec.Group == curr.Spec.Group {
existingIndex = j
break
}
}

if existingIndex >= 0 {
ret[existingIndex] = append(ret[existingIndex], curr)
sort.Sort(ByVersionPriority(ret[existingIndex]))
continue
}

ret = append(ret, []*v1.APIService{curr})
}

return ret
}

// ByGroupPriorityMinimum sorts with the highest group number first, then by name.
// This is not a simple reverse, because we want the name sorting to be alpha, not
// reverse alpha.
type ByGroupPriorityMinimum []*v1.APIService

func (s ByGroupPriorityMinimum) Len() int { return len(s) }
func (s ByGroupPriorityMinimum) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByGroupPriorityMinimum) Less(i, j int) bool {
if s[i].Spec.GroupPriorityMinimum != s[j].Spec.GroupPriorityMinimum {
return s[i].Spec.GroupPriorityMinimum > s[j].Spec.GroupPriorityMinimum
}
return s[i].Name < s[j].Name
}

// ByVersionPriority sorts with the highest version number first, then by name.
// This is not a simple reverse, because we want the name sorting to be alpha, not
// reverse alpha.
type ByVersionPriority []*v1.APIService

func (s ByVersionPriority) Len() int { return len(s) }
func (s ByVersionPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByVersionPriority) Less(i, j int) bool {
if s[i].Spec.VersionPriority != s[j].Spec.VersionPriority {
return s[i].Spec.VersionPriority > s[j].Spec.VersionPriority
}
return version.CompareKubeAwareVersionStrings(s[i].Spec.Version, s[j].Spec.Version) > 0
}

// APIServiceNameToGroupVersion returns the GroupVersion for a given apiServiceName. The name
// must be valid, but any object you get back from an informer will be valid.
func APIServiceNameToGroupVersion(apiServiceName string) schema.GroupVersion {
tokens := strings.SplitN(apiServiceName, ".", 2)
return schema.GroupVersion{Group: tokens[1], Version: tokens[0]}
}

// NewLocalAvailableAPIServiceCondition returns a condition for an available local APIService.
func NewLocalAvailableAPIServiceCondition() v1.APIServiceCondition {
return v1.APIServiceCondition{
Type: v1.Available,
Status: v1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: "Local",
Message: "Local APIServices are always available",
}
}

// SetAPIServiceCondition sets the status condition. It either overwrites the existing one or
// creates a new one
func SetAPIServiceCondition(apiService *v1.APIService, newCondition v1.APIServiceCondition) {
existingCondition := GetAPIServiceConditionByType(apiService, newCondition.Type)
if existingCondition == nil {
apiService.Status.Conditions = append(apiService.Status.Conditions, newCondition)
return
}

if existingCondition.Status != newCondition.Status {
existingCondition.Status = newCondition.Status
existingCondition.LastTransitionTime = newCondition.LastTransitionTime
}

existingCondition.Reason = newCondition.Reason
existingCondition.Message = newCondition.Message
}

// IsAPIServiceConditionTrue indicates if the condition is present and strictly true
func IsAPIServiceConditionTrue(apiService *v1.APIService, conditionType v1.APIServiceConditionType) bool {
condition := GetAPIServiceConditionByType(apiService, conditionType)
return condition != nil && condition.Status == v1.ConditionTrue
}

// GetAPIServiceConditionByType gets an *APIServiceCondition by APIServiceConditionType if present
func GetAPIServiceConditionByType(apiService *v1.APIService, conditionType v1.APIServiceConditionType) *v1.APIServiceCondition {
for i := range apiService.Status.Conditions {
if apiService.Status.Conditions[i].Type == conditionType {
return &apiService.Status.Conditions[i]
}
}
return nil
}
200 changes: 200 additions & 0 deletions pkg/apis/apiregistration/v1/helper/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
Copyright 2018 The Kubernetes 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 helper

import (
"reflect"
"testing"

"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
)

var (
a v1.APIServiceConditionType = "A"
b v1.APIServiceConditionType = "B"
c v1.APIServiceConditionType = "C"
)

func TestIsAPIServiceConditionTrue(t *testing.T) {
conditionATrue := makeNewAPIServiceCondition(a, "a reason", "a message", v1.ConditionTrue)
conditionAFalse := makeNewAPIServiceCondition(a, "a reason", "a message", v1.ConditionFalse)
tests := []*struct {
name string
apiService *v1.APIService
conditionType v1.APIServiceConditionType
expected bool
}{
{
name: "Should return false when condition of type is not present",
apiService: makeNewAPIService("v1", 100),
conditionType: a,
expected: false,
},
{
name: "Should return false when condition of type is present but status is not ConditionTrue",
apiService: makeNewAPIService("v1", 100, conditionAFalse),
conditionType: a,
expected: false,
},
{
name: "Should return false when condition of type is present but status is not ConditionTrue",
apiService: makeNewAPIService("v1", 100, conditionATrue),
conditionType: a,
expected: true,
},
}

for _, tc := range tests {
if isConditionTrue := IsAPIServiceConditionTrue(tc.apiService, tc.conditionType); isConditionTrue != tc.expected {
t.Errorf("expected condition of type %v to be %v, actually was %v",
tc.conditionType, isConditionTrue, tc.expected)

}
}
}

func TestSetAPIServiceCondition(t *testing.T) {
conditionA1 := makeNewAPIServiceCondition(a, "a1 reason", "a1 message", v1.ConditionTrue)
conditionA2 := makeNewAPIServiceCondition(a, "a2 reason", "a2 message", v1.ConditionTrue)
tests := []*struct {
name string
apiService *v1.APIService
conditionType v1.APIServiceConditionType
initialCondition *v1.APIServiceCondition
setCondition v1.APIServiceCondition
expectedCondition *v1.APIServiceCondition
}{
{
name: "Should set a new condition with type where previously there was no condition of that type",
apiService: makeNewAPIService("v1", 100),
conditionType: a,
initialCondition: nil,
setCondition: conditionA1,
expectedCondition: &conditionA1,
},
{
name: "Should override a condition of type, when a condition of that type existed previously",
apiService: makeNewAPIService("v1", 100, conditionA1),
conditionType: a,
initialCondition: &conditionA1,
setCondition: conditionA2,
expectedCondition: &conditionA2,
},
}

for _, tc := range tests {
startingCondition := GetAPIServiceConditionByType(tc.apiService, tc.conditionType)
if !reflect.DeepEqual(startingCondition, tc.initialCondition) {
t.Errorf("expected to find condition %s initially, actual was %s", tc.initialCondition, startingCondition)

}
SetAPIServiceCondition(tc.apiService, tc.setCondition)
actual := GetAPIServiceConditionByType(tc.apiService, tc.setCondition.Type)
if !reflect.DeepEqual(actual, tc.expectedCondition) {
t.Errorf("expected %s, actual %s", tc.expectedCondition, actual)
}
}
}

func TestSortedAPIServicesByVersion(t *testing.T) {
tests := []*struct {
name string
versions []string
expected []string
}{
{
name: "case1",
versions: []string{"v1", "v2"},
expected: []string{"v2", "v1"},
},
{
name: "case2",
versions: []string{"v2", "v10"},
expected: []string{"v10", "v2"},
},
{
name: "case3",
versions: []string{"v2", "v2beta1", "v10beta2", "v10beta1", "v10alpha1", "v1"},
expected: []string{"v2", "v1", "v10beta2", "v10beta1", "v2beta1", "v10alpha1"},
},
{
name: "case4",
versions: []string{"v1", "v2", "test", "foo10", "final", "foo2", "foo1"},
expected: []string{"v2", "v1", "final", "foo1", "foo10", "foo2", "test"},
},
{
name: "case5_from_documentation",
versions: []string{"v12alpha1", "v10", "v11beta2", "v10beta3", "v3beta1", "v2", "v11alpha2", "foo1", "v1", "foo10"},
expected: []string{"v10", "v2", "v1", "v11beta2", "v10beta3", "v3beta1", "v12alpha1", "v11alpha2", "foo1", "foo10"},
},
}

for _, tc := range tests {
apiServices := []*v1.APIService{}
for _, v := range tc.versions {
apiServices = append(apiServices, makeNewAPIService(v, 100))
}
sortedServices := SortedByGroupAndVersion(apiServices)
actual := []string{}
for _, s := range sortedServices[0] {
actual = append(actual, s.Spec.Version)
}
if !reflect.DeepEqual(tc.expected, actual) {
t.Errorf("expected %s, actual %s", tc.expected, actual)
}
}
}

func TestGetAPIServiceConditionByType(t *testing.T) {
conditionA := makeNewAPIServiceCondition(a, "a reason", "a message", v1.ConditionTrue)
conditionB := makeNewAPIServiceCondition(b, "b reason", "b message", v1.ConditionTrue)
tests := []*struct {
name string
apiService *v1.APIService
conditionType v1.APIServiceConditionType
expectedCondition *v1.APIServiceCondition
}{
{
name: "Should find a matching condition from apiService",
apiService: makeNewAPIService("v1", 100, conditionA, conditionB),
conditionType: a,
expectedCondition: &conditionA,
},
{
name: "Should not find a matching condition",
apiService: makeNewAPIService("v1", 100, conditionA),
conditionType: b,
expectedCondition: nil,
},
}

for _, tc := range tests {
actual := GetAPIServiceConditionByType(tc.apiService, tc.conditionType)
if !reflect.DeepEqual(tc.expectedCondition, actual) {
t.Errorf("expected %s, actual %s", tc.expectedCondition, actual)
}
}
}

func makeNewAPIService(version string, priority int32, conditions ...v1.APIServiceCondition) *v1.APIService {
status := v1.APIServiceStatus{Conditions: conditions}
return &v1.APIService{Spec: v1.APIServiceSpec{Version: version, VersionPriority: priority}, Status: status}
}

func makeNewAPIServiceCondition(conditionType v1.APIServiceConditionType, reason string, message string, status v1.ConditionStatus) v1.APIServiceCondition {
return v1.APIServiceCondition{Type: conditionType, Reason: reason, Message: message, Status: status}
}
Loading

0 comments on commit 8a45099

Please sign in to comment.