k8s.io/kubernetes@v1.29.3/test/e2e/framework/manifest/manifest.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package manifest
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"time"
    25  
    26  	appsv1 "k8s.io/api/apps/v1"
    27  	v1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  	utilyaml "k8s.io/apimachinery/pkg/util/yaml"
    31  	"k8s.io/client-go/kubernetes/scheme"
    32  	commonutils "k8s.io/kubernetes/test/e2e/common"
    33  	"k8s.io/kubernetes/test/e2e/framework"
    34  	e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
    35  )
    36  
    37  // PodFromManifest reads a .json/yaml file and returns the pod in it.
    38  func PodFromManifest(filename string) (*v1.Pod, error) {
    39  	var pod v1.Pod
    40  	data, err := e2etestfiles.Read(filename)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	json, err := utilyaml.ToJSON(data)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &pod); err != nil {
    50  		return nil, err
    51  	}
    52  	return &pod, nil
    53  }
    54  
    55  // SvcFromManifest reads a .json/yaml file and returns the service in it.
    56  func SvcFromManifest(fileName string) (*v1.Service, error) {
    57  	var svc v1.Service
    58  	data, err := e2etestfiles.Read(fileName)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	json, err := utilyaml.ToJSON(data)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &svc); err != nil {
    68  		return nil, err
    69  	}
    70  	return &svc, nil
    71  }
    72  
    73  // StatefulSetFromManifest returns a StatefulSet from a manifest stored in fileName in the Namespace indicated by ns.
    74  func StatefulSetFromManifest(fileName, ns string) (*appsv1.StatefulSet, error) {
    75  	var ss appsv1.StatefulSet
    76  	data, err := e2etestfiles.Read(fileName)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	statefulsetYaml := commonutils.SubstituteImageName(string(data))
    81  	json, err := utilyaml.ToJSON([]byte(statefulsetYaml))
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &ss); err != nil {
    86  		return nil, err
    87  	}
    88  	ss.Namespace = ns
    89  	if ss.Spec.Selector == nil {
    90  		ss.Spec.Selector = &metav1.LabelSelector{
    91  			MatchLabels: ss.Spec.Template.Labels,
    92  		}
    93  	}
    94  	return &ss, nil
    95  }
    96  
    97  // DaemonSetFromURL reads from a url and returns the daemonset in it.
    98  func DaemonSetFromURL(ctx context.Context, url string) (*appsv1.DaemonSet, error) {
    99  	framework.Logf("Parsing ds from %v", url)
   100  
   101  	var response *http.Response
   102  	var err error
   103  
   104  	for i := 1; i <= 5; i++ {
   105  		request, reqErr := http.NewRequestWithContext(ctx, "GET", url, nil)
   106  		if reqErr != nil {
   107  			err = reqErr
   108  			continue
   109  		}
   110  		response, err = http.DefaultClient.Do(request)
   111  		if err == nil && response.StatusCode == 200 {
   112  			break
   113  		}
   114  		time.Sleep(time.Duration(i) * time.Second)
   115  	}
   116  
   117  	if err != nil {
   118  		return nil, fmt.Errorf("Failed to get url: %w", err)
   119  	}
   120  	if response.StatusCode != 200 {
   121  		return nil, fmt.Errorf("invalid http response status: %v", response.StatusCode)
   122  	}
   123  	defer response.Body.Close()
   124  
   125  	data, err := io.ReadAll(response.Body)
   126  	if err != nil {
   127  		return nil, fmt.Errorf("Failed to read html response body: %w", err)
   128  	}
   129  	return DaemonSetFromData(data)
   130  }
   131  
   132  // DaemonSetFromData reads a byte slice and returns the daemonset in it.
   133  func DaemonSetFromData(data []byte) (*appsv1.DaemonSet, error) {
   134  	var ds appsv1.DaemonSet
   135  	dataJSON, err := utilyaml.ToJSON(data)
   136  	if err != nil {
   137  		return nil, fmt.Errorf("Failed to parse data to json: %w", err)
   138  	}
   139  
   140  	err = runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), dataJSON, &ds)
   141  	if err != nil {
   142  		return nil, fmt.Errorf("Failed to decode DaemonSet spec: %w", err)
   143  	}
   144  	return &ds, nil
   145  }