This commit is contained in:
Chao Xu 2016-11-18 13:31:44 -08:00
parent d0a725a522
commit 31ed340eec
8 changed files with 99 additions and 53 deletions

View File

@ -26,6 +26,7 @@ import (
"net" "net"
"k8s.io/kubernetes/pkg/apis/certificates" "k8s.io/kubernetes/pkg/apis/certificates"
"k8s.io/kubernetes/pkg/apis/certificates/v1alpha1"
) )
// ParseCSR extracts the CSR from the API object and decodes it. // ParseCSR extracts the CSR from the API object and decodes it.
@ -43,6 +44,21 @@ func ParseCSR(obj *certificates.CertificateSigningRequest) (*x509.CertificateReq
return csr, nil return csr, nil
} }
// ParseCSR extracts the CSR from the API object and decodes it.
func ParseCSRV1alpha1(obj *v1alpha1.CertificateSigningRequest) (*x509.CertificateRequest, error) {
// extract PEM from request object
pemBytes := obj.Spec.Request
block, _ := pem.Decode(pemBytes)
if block == nil || block.Type != "CERTIFICATE REQUEST" {
return nil, errors.New("PEM block type must be CERTIFICATE REQUEST")
}
csr, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
return nil, err
}
return csr, nil
}
// MakeCSR generates a PEM-encoded CSR using the supplied private key, subject, and SANs. // MakeCSR generates a PEM-encoded CSR using the supplied private key, subject, and SANs.
// All key types that are implemented via crypto.Signer are supported (This includes *rsa.PrivateKey and *ecdsa.PrivateKey.) // All key types that are implemented via crypto.Signer are supported (This includes *rsa.PrivateKey and *ecdsa.PrivateKey.)
func MakeCSR(privateKey interface{}, subject *pkix.Name, dnsSANs []string, ipSANs []net.IP) (csr []byte, err error) { func MakeCSR(privateKey interface{}, subject *pkix.Name, dnsSANs []string, ipSANs []net.IP) (csr []byte, err error) {

View File

@ -22,12 +22,13 @@ import (
"os" "os"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apimachinery/registered" "k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
) )
// LoadPodFromFile will read, decode, and return a Pod from a file. // LoadPodFromFile will read, decode, and return a Pod from a file.
func LoadPodFromFile(filePath string) (*api.Pod, error) { func LoadPodFromFile(filePath string) (*v1.Pod, error) {
if filePath == "" { if filePath == "" {
return nil, fmt.Errorf("file path not specified") return nil, fmt.Errorf("file path not specified")
} }
@ -38,9 +39,9 @@ func LoadPodFromFile(filePath string) (*api.Pod, error) {
if len(podDef) == 0 { if len(podDef) == 0 {
return nil, fmt.Errorf("file was empty: %s", filePath) return nil, fmt.Errorf("file was empty: %s", filePath)
} }
pod := &api.Pod{} pod := &v1.Pod{}
codec := api.Codecs.LegacyCodec(registered.GroupOrDie(api.GroupName).GroupVersion) codec := api.Codecs.LegacyCodec(registered.GroupOrDie(v1.GroupName).GroupVersion)
if err := runtime.DecodeInto(codec, podDef, pod); err != nil { if err := runtime.DecodeInto(codec, podDef, pod); err != nil {
return nil, fmt.Errorf("failed decoding file: %v", err) return nil, fmt.Errorf("failed decoding file: %v", err)
} }
@ -48,11 +49,11 @@ func LoadPodFromFile(filePath string) (*api.Pod, error) {
} }
// SavePodToFile will encode and save a pod to a given path & permissions // SavePodToFile will encode and save a pod to a given path & permissions
func SavePodToFile(pod *api.Pod, filePath string, perm os.FileMode) error { func SavePodToFile(pod *v1.Pod, filePath string, perm os.FileMode) error {
if filePath == "" { if filePath == "" {
return fmt.Errorf("file path not specified") return fmt.Errorf("file path not specified")
} }
codec := api.Codecs.LegacyCodec(registered.GroupOrDie(api.GroupName).GroupVersion) codec := api.Codecs.LegacyCodec(registered.GroupOrDie(v1.GroupName).GroupVersion)
data, err := runtime.Encode(codec, pod) data, err := runtime.Encode(codec, pod)
if err != nil { if err != nil {
return fmt.Errorf("failed encoding pod: %v", err) return fmt.Errorf("failed encoding pod: %v", err)

View File

@ -27,7 +27,8 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" "k8s.io/kubernetes/pkg/api/v1"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
"k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/types"
) )
@ -52,7 +53,7 @@ func GetHostname(hostnameOverride string) string {
// GetPreferredNodeAddress returns the address of the provided node, using the provided preference order. // GetPreferredNodeAddress returns the address of the provided node, using the provided preference order.
// If none of the preferred address types are found, an error is returned. // If none of the preferred address types are found, an error is returned.
func GetPreferredNodeAddress(node *api.Node, preferredAddressTypes []api.NodeAddressType) (string, error) { func GetPreferredNodeAddress(node *v1.Node, preferredAddressTypes []v1.NodeAddressType) (string, error) {
for _, addressType := range preferredAddressTypes { for _, addressType := range preferredAddressTypes {
for _, address := range node.Status.Addresses { for _, address := range node.Status.Addresses {
if address.Type == addressType { if address.Type == addressType {
@ -60,7 +61,7 @@ func GetPreferredNodeAddress(node *api.Node, preferredAddressTypes []api.NodeAdd
} }
} }
// If hostname was requested and no Hostname address was registered... // If hostname was requested and no Hostname address was registered...
if addressType == api.NodeHostName { if addressType == v1.NodeHostName {
// ...fall back to the kubernetes.io/hostname label for compatibility with kubelets before 1.5 // ...fall back to the kubernetes.io/hostname label for compatibility with kubelets before 1.5
if hostname, ok := node.Labels[unversioned.LabelHostname]; ok && len(hostname) > 0 { if hostname, ok := node.Labels[unversioned.LabelHostname]; ok && len(hostname) > 0 {
return hostname, nil return hostname, nil
@ -74,7 +75,29 @@ func GetPreferredNodeAddress(node *api.Node, preferredAddressTypes []api.NodeAdd
// 1. NodeInternalIP // 1. NodeInternalIP
// 2. NodeExternalIP // 2. NodeExternalIP
// 3. NodeLegacyHostIP // 3. NodeLegacyHostIP
func GetNodeHostIP(node *api.Node) (net.IP, error) { func GetNodeHostIP(node *v1.Node) (net.IP, error) {
addresses := node.Status.Addresses
addressMap := make(map[v1.NodeAddressType][]v1.NodeAddress)
for i := range addresses {
addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
}
if addresses, ok := addressMap[v1.NodeInternalIP]; ok {
return net.ParseIP(addresses[0].Address), nil
}
if addresses, ok := addressMap[v1.NodeExternalIP]; ok {
return net.ParseIP(addresses[0].Address), nil
}
if addresses, ok := addressMap[v1.NodeLegacyHostIP]; ok {
return net.ParseIP(addresses[0].Address), nil
}
return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses)
}
// InternalGetNodeHostIP returns the provided node's IP, based on the priority:
// 1. NodeInternalIP
// 2. NodeExternalIP
// 3. NodeLegacyHostIP
func InternalGetNodeHostIP(node *api.Node) (net.IP, error) {
addresses := node.Status.Addresses addresses := node.Status.Addresses
addressMap := make(map[api.NodeAddressType][]api.NodeAddress) addressMap := make(map[api.NodeAddressType][]api.NodeAddress)
for i := range addresses { for i := range addresses {
@ -94,7 +117,7 @@ func GetNodeHostIP(node *api.Node) (net.IP, error) {
// Helper function that builds a string identifier that is unique per failure-zone // Helper function that builds a string identifier that is unique per failure-zone
// Returns empty-string for no zone // Returns empty-string for no zone
func GetZoneKey(node *api.Node) string { func GetZoneKey(node *v1.Node) string {
labels := node.Labels labels := node.Labels
if labels == nil { if labels == nil {
return "" return ""
@ -114,9 +137,9 @@ func GetZoneKey(node *api.Node) string {
} }
// SetNodeCondition updates specific node condition with patch operation. // SetNodeCondition updates specific node condition with patch operation.
func SetNodeCondition(c clientset.Interface, node types.NodeName, condition api.NodeCondition) error { func SetNodeCondition(c clientset.Interface, node types.NodeName, condition v1.NodeCondition) error {
generatePatch := func(condition api.NodeCondition) ([]byte, error) { generatePatch := func(condition v1.NodeCondition) ([]byte, error) {
raw, err := json.Marshal(&[]api.NodeCondition{condition}) raw, err := json.Marshal(&[]v1.NodeCondition{condition})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -19,15 +19,15 @@ package node
import ( import (
"testing" "testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
) )
func TestGetPreferredAddress(t *testing.T) { func TestGetPreferredAddress(t *testing.T) {
testcases := map[string]struct { testcases := map[string]struct {
Labels map[string]string Labels map[string]string
Addresses []api.NodeAddress Addresses []v1.NodeAddress
Preferences []api.NodeAddressType Preferences []v1.NodeAddressType
ExpectErr string ExpectErr string
ExpectAddress string ExpectAddress string
@ -36,44 +36,44 @@ func TestGetPreferredAddress(t *testing.T) {
ExpectErr: "no preferred addresses found; known addresses: []", ExpectErr: "no preferred addresses found; known addresses: []",
}, },
"missing address": { "missing address": {
Addresses: []api.NodeAddress{ Addresses: []v1.NodeAddress{
{Type: api.NodeInternalIP, Address: "1.2.3.4"}, {Type: v1.NodeInternalIP, Address: "1.2.3.4"},
}, },
Preferences: []api.NodeAddressType{api.NodeHostName}, Preferences: []v1.NodeAddressType{v1.NodeHostName},
ExpectErr: "no preferred addresses found; known addresses: [{InternalIP 1.2.3.4}]", ExpectErr: "no preferred addresses found; known addresses: [{InternalIP 1.2.3.4}]",
}, },
"found address": { "found address": {
Addresses: []api.NodeAddress{ Addresses: []v1.NodeAddress{
{Type: api.NodeInternalIP, Address: "1.2.3.4"}, {Type: v1.NodeInternalIP, Address: "1.2.3.4"},
{Type: api.NodeExternalIP, Address: "1.2.3.5"}, {Type: v1.NodeExternalIP, Address: "1.2.3.5"},
{Type: api.NodeExternalIP, Address: "1.2.3.7"}, {Type: v1.NodeExternalIP, Address: "1.2.3.7"},
}, },
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP}, Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
ExpectAddress: "1.2.3.5", ExpectAddress: "1.2.3.5",
}, },
"found hostname address": { "found hostname address": {
Labels: map[string]string{unversioned.LabelHostname: "label-hostname"}, Labels: map[string]string{unversioned.LabelHostname: "label-hostname"},
Addresses: []api.NodeAddress{ Addresses: []v1.NodeAddress{
{Type: api.NodeExternalIP, Address: "1.2.3.5"}, {Type: v1.NodeExternalIP, Address: "1.2.3.5"},
{Type: api.NodeHostName, Address: "status-hostname"}, {Type: v1.NodeHostName, Address: "status-hostname"},
}, },
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP}, Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
ExpectAddress: "status-hostname", ExpectAddress: "status-hostname",
}, },
"found label address": { "found label address": {
Labels: map[string]string{unversioned.LabelHostname: "label-hostname"}, Labels: map[string]string{unversioned.LabelHostname: "label-hostname"},
Addresses: []api.NodeAddress{ Addresses: []v1.NodeAddress{
{Type: api.NodeExternalIP, Address: "1.2.3.5"}, {Type: v1.NodeExternalIP, Address: "1.2.3.5"},
}, },
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP}, Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
ExpectAddress: "label-hostname", ExpectAddress: "label-hostname",
}, },
} }
for k, tc := range testcases { for k, tc := range testcases {
node := &api.Node{ node := &v1.Node{
ObjectMeta: api.ObjectMeta{Labels: tc.Labels}, ObjectMeta: v1.ObjectMeta{Labels: tc.Labels},
Status: api.NodeStatus{Addresses: tc.Addresses}, Status: v1.NodeStatus{Addresses: tc.Addresses},
} }
address, err := GetPreferredNodeAddress(node, tc.Preferences) address, err := GetPreferredNodeAddress(node, tc.Preferences)
errString := "" errString := ""

View File

@ -25,13 +25,21 @@ import (
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/errors"
unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" "k8s.io/kubernetes/pkg/api/v1"
v1core "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/typed/core/v1"
errorsutil "k8s.io/kubernetes/pkg/util/errors" errorsutil "k8s.io/kubernetes/pkg/util/errors"
hashutil "k8s.io/kubernetes/pkg/util/hash" hashutil "k8s.io/kubernetes/pkg/util/hash"
"k8s.io/kubernetes/pkg/util/wait" "k8s.io/kubernetes/pkg/util/wait"
) )
func GetPodTemplateSpecHash(template api.PodTemplateSpec) uint32 { func GetPodTemplateSpecHash(template v1.PodTemplateSpec) uint32 {
podTemplateSpecHasher := adler32.New()
hashutil.DeepHashObject(podTemplateSpecHasher, template)
return podTemplateSpecHasher.Sum32()
}
// TODO: remove the duplicate
func GetInternalPodTemplateSpecHash(template api.PodTemplateSpec) uint32 {
podTemplateSpecHasher := adler32.New() podTemplateSpecHasher := adler32.New()
hashutil.DeepHashObject(podTemplateSpecHasher, template) hashutil.DeepHashObject(podTemplateSpecHasher, template)
return podTemplateSpecHasher.Sum32() return podTemplateSpecHasher.Sum32()
@ -39,11 +47,11 @@ func GetPodTemplateSpecHash(template api.PodTemplateSpec) uint32 {
// TODO: use client library instead when it starts to support update retries // TODO: use client library instead when it starts to support update retries
// see https://github.com/kubernetes/kubernetes/issues/21479 // see https://github.com/kubernetes/kubernetes/issues/21479
type updatePodFunc func(pod *api.Pod) error type updatePodFunc func(pod *v1.Pod) error
// UpdatePodWithRetries updates a pod with given applyUpdate function. Note that pod not found error is ignored. // UpdatePodWithRetries updates a pod with given applyUpdate function. Note that pod not found error is ignored.
// The returned bool value can be used to tell if the pod is actually updated. // The returned bool value can be used to tell if the pod is actually updated.
func UpdatePodWithRetries(podClient unversionedcore.PodInterface, pod *api.Pod, applyUpdate updatePodFunc) (*api.Pod, bool, error) { func UpdatePodWithRetries(podClient v1core.PodInterface, pod *v1.Pod, applyUpdate updatePodFunc) (*v1.Pod, bool, error) {
var err error var err error
var podUpdated bool var podUpdated bool
oldPod := pod oldPod := pod
@ -89,8 +97,8 @@ func UpdatePodWithRetries(podClient unversionedcore.PodInterface, pod *api.Pod,
} }
// Filter uses the input function f to filter the given pod list, and return the filtered pods // Filter uses the input function f to filter the given pod list, and return the filtered pods
func Filter(podList *api.PodList, f func(api.Pod) bool) []api.Pod { func Filter(podList *v1.PodList, f func(v1.Pod) bool) []v1.Pod {
pods := make([]api.Pod, 0) pods := make([]v1.Pod, 0)
for _, p := range podList.Items { for _, p := range podList.Items {
if f(p) { if f(p) {
pods = append(pods, p) pods = append(pods, p)

View File

@ -21,11 +21,11 @@ import (
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions" "k8s.io/kubernetes/pkg/api/v1"
unversionedextensions "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/extensions/internalversion" extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
unversionedextensions "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/typed/extensions/v1beta1"
"k8s.io/kubernetes/pkg/labels" "k8s.io/kubernetes/pkg/labels"
errorsutil "k8s.io/kubernetes/pkg/util/errors" errorsutil "k8s.io/kubernetes/pkg/util/errors"
labelsutil "k8s.io/kubernetes/pkg/util/labels" labelsutil "k8s.io/kubernetes/pkg/util/labels"
@ -88,14 +88,14 @@ func UpdateRSWithRetries(rsClient unversionedextensions.ReplicaSetInterface, rs
func GetPodTemplateSpecHash(rs *extensions.ReplicaSet) string { func GetPodTemplateSpecHash(rs *extensions.ReplicaSet) string {
meta := rs.Spec.Template.ObjectMeta meta := rs.Spec.Template.ObjectMeta
meta.Labels = labelsutil.CloneAndRemoveLabel(meta.Labels, extensions.DefaultDeploymentUniqueLabelKey) meta.Labels = labelsutil.CloneAndRemoveLabel(meta.Labels, extensions.DefaultDeploymentUniqueLabelKey)
return fmt.Sprintf("%d", podutil.GetPodTemplateSpecHash(api.PodTemplateSpec{ return fmt.Sprintf("%d", podutil.GetPodTemplateSpecHash(v1.PodTemplateSpec{
ObjectMeta: meta, ObjectMeta: meta,
Spec: rs.Spec.Template.Spec, Spec: rs.Spec.Template.Spec,
})) }))
} }
// MatchingPodsFunc returns a filter function for pods with matching labels // MatchingPodsFunc returns a filter function for pods with matching labels
func MatchingPodsFunc(rs *extensions.ReplicaSet) (func(api.Pod) bool, error) { func MatchingPodsFunc(rs *extensions.ReplicaSet) (func(v1.Pod) bool, error) {
if rs == nil { if rs == nil {
return nil, nil return nil, nil
} }
@ -103,7 +103,7 @@ func MatchingPodsFunc(rs *extensions.ReplicaSet) (func(api.Pod) bool, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err) return nil, fmt.Errorf("invalid label selector: %v", err)
} }
return func(pod api.Pod) bool { return func(pod v1.Pod) bool {
podLabelsSelector := labels.Set(pod.ObjectMeta.Labels) podLabelsSelector := labels.Set(pod.ObjectMeta.Labels)
return selector.Matches(podLabelsSelector) return selector.Matches(podLabelsSelector)
}, nil }, nil

View File

@ -18,12 +18,10 @@ package system
import ( import (
"regexp" "regexp"
"k8s.io/kubernetes/pkg/api"
) )
// TODO: find a better way of figuring out if given node is a registered master. // TODO: find a better way of figuring out if given node is a registered master.
func IsMasterNode(node *api.Node) bool { func IsMasterNode(nodeName string) bool {
r := regexp.MustCompile("master(-...)?$") r := regexp.MustCompile("master(-...)?$")
return r.MatchString(node.Name) return r.MatchString(nodeName)
} }

View File

@ -19,7 +19,7 @@ package system
import ( import (
"testing" "testing"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/v1"
) )
func TestIsMasterNode(t *testing.T) { func TestIsMasterNode(t *testing.T) {
@ -37,8 +37,8 @@ func TestIsMasterNode(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
node := api.Node{ObjectMeta: api.ObjectMeta{Name: tc.input}} node := v1.Node{ObjectMeta: v1.ObjectMeta{Name: tc.input}}
res := IsMasterNode(&node) res := IsMasterNode(node.Name)
if res != tc.result { if res != tc.result {
t.Errorf("case \"%s\": expected %t, got %t", tc.input, tc.result, res) t.Errorf("case \"%s\": expected %t, got %t", tc.input, tc.result, res)
} }