mirror of https://github.com/k3s-io/k3s
pkg/util
parent
d0a725a522
commit
31ed340eec
|
@ -26,6 +26,7 @@ import (
|
|||
"net"
|
||||
|
||||
"k8s.io/kubernetes/pkg/apis/certificates"
|
||||
"k8s.io/kubernetes/pkg/apis/certificates/v1alpha1"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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) {
|
||||
|
|
|
@ -22,12 +22,13 @@ import (
|
|||
"os"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// 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 == "" {
|
||||
return nil, fmt.Errorf("file path not specified")
|
||||
}
|
||||
|
@ -38,9 +39,9 @@ func LoadPodFromFile(filePath string) (*api.Pod, error) {
|
|||
if len(podDef) == 0 {
|
||||
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 {
|
||||
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
|
||||
func SavePodToFile(pod *api.Pod, filePath string, perm os.FileMode) error {
|
||||
func SavePodToFile(pod *v1.Pod, filePath string, perm os.FileMode) error {
|
||||
if filePath == "" {
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed encoding pod: %v", err)
|
||||
|
|
|
@ -27,7 +27,8 @@ import (
|
|||
"github.com/golang/glog"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -52,7 +53,7 @@ func GetHostname(hostnameOverride string) string {
|
|||
|
||||
// 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.
|
||||
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 _, address := range node.Status.Addresses {
|
||||
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 addressType == api.NodeHostName {
|
||||
if addressType == v1.NodeHostName {
|
||||
// ...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 {
|
||||
return hostname, nil
|
||||
|
@ -74,7 +75,29 @@ func GetPreferredNodeAddress(node *api.Node, preferredAddressTypes []api.NodeAdd
|
|||
// 1. NodeInternalIP
|
||||
// 2. NodeExternalIP
|
||||
// 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
|
||||
addressMap := make(map[api.NodeAddressType][]api.NodeAddress)
|
||||
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
|
||||
// Returns empty-string for no zone
|
||||
func GetZoneKey(node *api.Node) string {
|
||||
func GetZoneKey(node *v1.Node) string {
|
||||
labels := node.Labels
|
||||
if labels == nil {
|
||||
return ""
|
||||
|
@ -114,9 +137,9 @@ func GetZoneKey(node *api.Node) string {
|
|||
}
|
||||
|
||||
// SetNodeCondition updates specific node condition with patch operation.
|
||||
func SetNodeCondition(c clientset.Interface, node types.NodeName, condition api.NodeCondition) error {
|
||||
generatePatch := func(condition api.NodeCondition) ([]byte, error) {
|
||||
raw, err := json.Marshal(&[]api.NodeCondition{condition})
|
||||
func SetNodeCondition(c clientset.Interface, node types.NodeName, condition v1.NodeCondition) error {
|
||||
generatePatch := func(condition v1.NodeCondition) ([]byte, error) {
|
||||
raw, err := json.Marshal(&[]v1.NodeCondition{condition})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -19,15 +19,15 @@ package node
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
)
|
||||
|
||||
func TestGetPreferredAddress(t *testing.T) {
|
||||
testcases := map[string]struct {
|
||||
Labels map[string]string
|
||||
Addresses []api.NodeAddress
|
||||
Preferences []api.NodeAddressType
|
||||
Addresses []v1.NodeAddress
|
||||
Preferences []v1.NodeAddressType
|
||||
|
||||
ExpectErr string
|
||||
ExpectAddress string
|
||||
|
@ -36,44 +36,44 @@ func TestGetPreferredAddress(t *testing.T) {
|
|||
ExpectErr: "no preferred addresses found; known addresses: []",
|
||||
},
|
||||
"missing address": {
|
||||
Addresses: []api.NodeAddress{
|
||||
{Type: api.NodeInternalIP, Address: "1.2.3.4"},
|
||||
Addresses: []v1.NodeAddress{
|
||||
{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}]",
|
||||
},
|
||||
"found address": {
|
||||
Addresses: []api.NodeAddress{
|
||||
{Type: api.NodeInternalIP, Address: "1.2.3.4"},
|
||||
{Type: api.NodeExternalIP, Address: "1.2.3.5"},
|
||||
{Type: api.NodeExternalIP, Address: "1.2.3.7"},
|
||||
Addresses: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "1.2.3.4"},
|
||||
{Type: v1.NodeExternalIP, Address: "1.2.3.5"},
|
||||
{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",
|
||||
},
|
||||
"found hostname address": {
|
||||
Labels: map[string]string{unversioned.LabelHostname: "label-hostname"},
|
||||
Addresses: []api.NodeAddress{
|
||||
{Type: api.NodeExternalIP, Address: "1.2.3.5"},
|
||||
{Type: api.NodeHostName, Address: "status-hostname"},
|
||||
Addresses: []v1.NodeAddress{
|
||||
{Type: v1.NodeExternalIP, Address: "1.2.3.5"},
|
||||
{Type: v1.NodeHostName, Address: "status-hostname"},
|
||||
},
|
||||
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP},
|
||||
Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
|
||||
ExpectAddress: "status-hostname",
|
||||
},
|
||||
"found label address": {
|
||||
Labels: map[string]string{unversioned.LabelHostname: "label-hostname"},
|
||||
Addresses: []api.NodeAddress{
|
||||
{Type: api.NodeExternalIP, Address: "1.2.3.5"},
|
||||
Addresses: []v1.NodeAddress{
|
||||
{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",
|
||||
},
|
||||
}
|
||||
|
||||
for k, tc := range testcases {
|
||||
node := &api.Node{
|
||||
ObjectMeta: api.ObjectMeta{Labels: tc.Labels},
|
||||
Status: api.NodeStatus{Addresses: tc.Addresses},
|
||||
node := &v1.Node{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: tc.Labels},
|
||||
Status: v1.NodeStatus{Addresses: tc.Addresses},
|
||||
}
|
||||
address, err := GetPreferredNodeAddress(node, tc.Preferences)
|
||||
errString := ""
|
||||
|
|
|
@ -25,13 +25,21 @@ import (
|
|||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"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"
|
||||
hashutil "k8s.io/kubernetes/pkg/util/hash"
|
||||
"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()
|
||||
hashutil.DeepHashObject(podTemplateSpecHasher, template)
|
||||
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
|
||||
// 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.
|
||||
// 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 podUpdated bool
|
||||
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
|
||||
func Filter(podList *api.PodList, f func(api.Pod) bool) []api.Pod {
|
||||
pods := make([]api.Pod, 0)
|
||||
func Filter(podList *v1.PodList, f func(v1.Pod) bool) []v1.Pod {
|
||||
pods := make([]v1.Pod, 0)
|
||||
for _, p := range podList.Items {
|
||||
if f(p) {
|
||||
pods = append(pods, p)
|
||||
|
|
|
@ -21,11 +21,11 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
unversionedextensions "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/extensions/internalversion"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
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"
|
||||
errorsutil "k8s.io/kubernetes/pkg/util/errors"
|
||||
labelsutil "k8s.io/kubernetes/pkg/util/labels"
|
||||
|
@ -88,14 +88,14 @@ func UpdateRSWithRetries(rsClient unversionedextensions.ReplicaSetInterface, rs
|
|||
func GetPodTemplateSpecHash(rs *extensions.ReplicaSet) string {
|
||||
meta := rs.Spec.Template.ObjectMeta
|
||||
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,
|
||||
Spec: rs.Spec.Template.Spec,
|
||||
}))
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ func MatchingPodsFunc(rs *extensions.ReplicaSet) (func(api.Pod) bool, error) {
|
|||
if err != nil {
|
||||
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)
|
||||
return selector.Matches(podLabelsSelector)
|
||||
}, nil
|
||||
|
|
|
@ -18,12 +18,10 @@ package system
|
|||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
// 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(-...)?$")
|
||||
return r.MatchString(node.Name)
|
||||
return r.MatchString(nodeName)
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ package system
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
)
|
||||
|
||||
func TestIsMasterNode(t *testing.T) {
|
||||
|
@ -37,8 +37,8 @@ func TestIsMasterNode(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
node := api.Node{ObjectMeta: api.ObjectMeta{Name: tc.input}}
|
||||
res := IsMasterNode(&node)
|
||||
node := v1.Node{ObjectMeta: v1.ObjectMeta{Name: tc.input}}
|
||||
res := IsMasterNode(node.Name)
|
||||
if res != tc.result {
|
||||
t.Errorf("case \"%s\": expected %t, got %t", tc.input, tc.result, res)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue