mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	- Remove string decode logic. It's not really helping to find the conflict ports, and it's expensive to do encoding/decoding - Not to parse the container ports information in predicate meta, use straight []*v1.ContainerPort - Use better data structure to search port conflict based on ip addresses - Collect scattered source code into common place
		
			
				
	
	
		
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
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 predicates
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	"k8s.io/api/core/v1"
 | 
						|
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						|
	"k8s.io/apimachinery/pkg/labels"
 | 
						|
)
 | 
						|
 | 
						|
// ExampleUtils is a https://blog.golang.org/examples styled unit test.
 | 
						|
func ExampleFindLabelsInSet() {
 | 
						|
	labelSubset := labels.Set{}
 | 
						|
	labelSubset["label1"] = "value1"
 | 
						|
	labelSubset["label2"] = "value2"
 | 
						|
	// Lets make believe that these pods are on the cluster.
 | 
						|
	// Utility functions will inspect their labels, filter them, and so on.
 | 
						|
	nsPods := []*v1.Pod{
 | 
						|
		{
 | 
						|
			ObjectMeta: metav1.ObjectMeta{
 | 
						|
				Name:      "pod1",
 | 
						|
				Namespace: "ns1",
 | 
						|
				Labels: map[string]string{
 | 
						|
					"label1": "wontSeeThis",
 | 
						|
					"label2": "wontSeeThis",
 | 
						|
					"label3": "will_see_this",
 | 
						|
				},
 | 
						|
			},
 | 
						|
		}, // first pod which will be used via the utilities
 | 
						|
		{
 | 
						|
			ObjectMeta: metav1.ObjectMeta{
 | 
						|
				Name:      "pod2",
 | 
						|
				Namespace: "ns1",
 | 
						|
			},
 | 
						|
		},
 | 
						|
 | 
						|
		{
 | 
						|
			ObjectMeta: metav1.ObjectMeta{
 | 
						|
				Name: "pod3ThatWeWontSee",
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
	fmt.Println(FindLabelsInSet([]string{"label1", "label2", "label3"}, nsPods[0].ObjectMeta.Labels)["label3"])
 | 
						|
	AddUnsetLabelsToMap(labelSubset, []string{"label1", "label2", "label3"}, nsPods[0].ObjectMeta.Labels)
 | 
						|
	fmt.Println(labelSubset)
 | 
						|
 | 
						|
	for _, pod := range FilterPodsByNamespace(nsPods, "ns1") {
 | 
						|
		fmt.Print(pod.Name, ",")
 | 
						|
	}
 | 
						|
	// Output:
 | 
						|
	// will_see_this
 | 
						|
	// label1=value1,label2=value2,label3=will_see_this
 | 
						|
	// pod1,pod2,
 | 
						|
}
 |