github.phpd.cn/cilium/cilium@v1.6.12/test/helpers/policygen/actions.go (about)

     1  // Copyright 2017 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package policygen
    15  
    16  import (
    17  	"fmt"
    18  	"net/http"
    19  
    20  	"github.com/cilium/cilium/test/helpers"
    21  
    22  	log "github.com/sirupsen/logrus"
    23  )
    24  
    25  // HTTPAction runs a helpers.CurlFail from specified pod to a specified target.
    26  // It needs a `helpers.Kubectl` instance to run the command in the pod. It
    27  // returns a ResultType struct.
    28  func HTTPAction(srcPod string, target string, kub *helpers.Kubectl) ResultType {
    29  	command := fmt.Sprintf(
    30  		"%s exec -n %s %s 2>/dev/null -- %s --output /dev/stout -w '%%{http_code}'",
    31  		helpers.KubectlCmd, helpers.DefaultNamespace,
    32  		srcPod, helpers.CurlFail(target))
    33  
    34  	log.Infof("Executing HTTPAction '%s'", command)
    35  	res := kub.Exec(command)
    36  	if res.WasSuccessful() {
    37  		return ResultOK
    38  	}
    39  	// Curl exitcodes are described in https://curl.haxx.se/libcurl/c/libcurl-errors.html
    40  	switch exitCode := res.GetExitCode(); exitCode {
    41  	case 28: //CURLE_OPERATION_TIMEDOUT (28)
    42  		return ResultTimeout
    43  	case 22: //CURLE_HTTP_RETURNED_ERROR
    44  		val, err := res.IntOutput()
    45  		if err != nil {
    46  			return ResultAuth
    47  		}
    48  		if val == http.StatusServiceUnavailable {
    49  			// This is for case  "L3:No Policy L4:Ingress Port 80 UDP L7:Egress
    50  			// policy to /private/" where the cilium egress proxy cannot connect
    51  			// to endpoint due timeout and return back a 503.
    52  			return ResultTimeout
    53  		}
    54  		return ResultAuth
    55  	default:
    56  		log.Infof("HTTPAction returned unexpected exit code %d", exitCode)
    57  	}
    58  	return ResultOK
    59  }
    60  
    61  // HTTPActionPrivate runs a HTTPAction to /private/ using destTargetDetails
    62  func HTTPActionPrivate(srcPod string, dest TargetDetails, kub *helpers.Kubectl) ResultType {
    63  	return HTTPAction(
    64  		srcPod,
    65  		fmt.Sprintf("http://%s/private", dest),
    66  		kub)
    67  }
    68  
    69  // HTTPActionPublic runs a CurlAction to /public/ using destTargetDetails
    70  func HTTPActionPublic(srcPod string, dest TargetDetails, kub *helpers.Kubectl) ResultType {
    71  	return HTTPAction(
    72  		srcPod,
    73  		fmt.Sprintf("http://%s/public", dest),
    74  		kub)
    75  }
    76  
    77  // NetPerfAction TODO make this function (GH-2029)
    78  func NetPerfAction(srcPod string, dest TargetDetails, kub *helpers.Kubectl) ResultType {
    79  	return ResultType{}
    80  }
    81  
    82  // PingAction executes a ping from the `srcPod` to the dest using Kubectl
    83  // object. Returns a ResultType corresponding to the exit code of the ping
    84  // command.
    85  func PingAction(srcPod string, dest TargetDetails, kub *helpers.Kubectl) ResultType {
    86  	command := fmt.Sprintf("%s exec -n %s %s -- %s",
    87  		helpers.KubectlCmd, helpers.DefaultNamespace,
    88  		srcPod, helpers.Ping(string(dest.IP)))
    89  
    90  	res := kub.Exec(command)
    91  	if res.WasSuccessful() {
    92  		return ResultOK
    93  	}
    94  	return ResultTimeout
    95  }