ingress.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package gce
  14. import (
  15. "context"
  16. "crypto/sha256"
  17. "encoding/json"
  18. "fmt"
  19. "net/http"
  20. "os/exec"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/onsi/ginkgo"
  25. compute "google.golang.org/api/compute/v1"
  26. "google.golang.org/api/googleapi"
  27. v1 "k8s.io/api/core/v1"
  28. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  29. "k8s.io/apimachinery/pkg/util/wait"
  30. clientset "k8s.io/client-go/kubernetes"
  31. "k8s.io/kubernetes/test/e2e/framework"
  32. e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
  33. utilexec "k8s.io/utils/exec"
  34. )
  35. const (
  36. // Name of the config-map and key the ingress controller stores its uid in.
  37. uidConfigMap = "ingress-uid"
  38. uidKey = "uid"
  39. // all cloud resources created by the ingress controller start with this
  40. // prefix.
  41. k8sPrefix = "k8s-"
  42. // clusterDelimiter is the delimiter used by the ingress controller
  43. // to split uid from other naming/metadata.
  44. clusterDelimiter = "--"
  45. // Cloud resources created by the ingress controller older than this
  46. // are automatically purged to prevent running out of quota.
  47. // TODO(37335): write soak tests and bump this up to a week.
  48. maxAge = 48 * time.Hour
  49. // GCE only allows names < 64 characters, and the loadbalancer controller inserts
  50. // a single character of padding.
  51. nameLenLimit = 62
  52. negBackend = backendType("networkEndpointGroup")
  53. igBackend = backendType("instanceGroup")
  54. )
  55. type backendType string
  56. // IngressController manages implementation details of Ingress on GCE/GKE.
  57. type IngressController struct {
  58. Ns string
  59. UID string
  60. staticIPName string
  61. Client clientset.Interface
  62. Cloud framework.CloudConfig
  63. }
  64. // CleanupIngressController calls cont.CleanupIngressControllerWithTimeout with hard-coded timeout
  65. func (cont *IngressController) CleanupIngressController() error {
  66. return cont.CleanupIngressControllerWithTimeout(e2eservice.LoadBalancerCleanupTimeout)
  67. }
  68. // CleanupIngressControllerWithTimeout calls the IngressController.Cleanup(false)
  69. // followed with deleting the static ip, and then a final IngressController.Cleanup(true)
  70. func (cont *IngressController) CleanupIngressControllerWithTimeout(timeout time.Duration) error {
  71. pollErr := wait.Poll(5*time.Second, timeout, func() (bool, error) {
  72. if err := cont.Cleanup(false); err != nil {
  73. framework.Logf("Monitoring glbc's cleanup of gce resources:\n%v", err)
  74. return false, nil
  75. }
  76. return true, nil
  77. })
  78. // Always try to cleanup even if pollErr == nil, because the cleanup
  79. // routine also purges old leaked resources based on creation timestamp.
  80. ginkgo.By("Performing final delete of any remaining resources")
  81. if cleanupErr := cont.Cleanup(true); cleanupErr != nil {
  82. ginkgo.By(fmt.Sprintf("WARNING: possibly leaked resources: %v\n", cleanupErr))
  83. } else {
  84. ginkgo.By("No resources leaked.")
  85. }
  86. // Static-IP allocated on behalf of the test, never deleted by the
  87. // controller. Delete this IP only after the controller has had a chance
  88. // to cleanup or it might interfere with the controller, causing it to
  89. // throw out confusing events.
  90. if ipErr := wait.Poll(5*time.Second, 1*time.Minute, func() (bool, error) {
  91. if err := cont.deleteStaticIPs(); err != nil {
  92. framework.Logf("Failed to delete static-ip: %v\n", err)
  93. return false, nil
  94. }
  95. return true, nil
  96. }); ipErr != nil {
  97. // If this is a persistent error, the suite will fail when we run out
  98. // of quota anyway.
  99. ginkgo.By(fmt.Sprintf("WARNING: possibly leaked static IP: %v\n", ipErr))
  100. }
  101. // Logging that the GLBC failed to cleanup GCE resources on ingress deletion
  102. // See kubernetes/ingress#431
  103. if pollErr != nil {
  104. return fmt.Errorf("error: L7 controller failed to delete all cloud resources on time. %v", pollErr)
  105. }
  106. return nil
  107. }
  108. func (cont *IngressController) getL7AddonUID() (string, error) {
  109. framework.Logf("Retrieving UID from config map: %v/%v", metav1.NamespaceSystem, uidConfigMap)
  110. cm, err := cont.Client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.TODO(), uidConfigMap, metav1.GetOptions{})
  111. if err != nil {
  112. return "", err
  113. }
  114. if uid, ok := cm.Data[uidKey]; ok {
  115. return uid, nil
  116. }
  117. return "", fmt.Errorf("Could not find cluster UID for L7 addon pod")
  118. }
  119. // ListGlobalForwardingRules returns a list of global forwarding rules
  120. func (cont *IngressController) ListGlobalForwardingRules() []*compute.ForwardingRule {
  121. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  122. fwdList := []*compute.ForwardingRule{}
  123. l, err := gceCloud.ListGlobalForwardingRules()
  124. framework.ExpectNoError(err)
  125. for _, fwd := range l {
  126. if cont.isOwned(fwd.Name) {
  127. fwdList = append(fwdList, fwd)
  128. }
  129. }
  130. return fwdList
  131. }
  132. func (cont *IngressController) deleteForwardingRule(del bool) string {
  133. msg := ""
  134. fwList := []compute.ForwardingRule{}
  135. for _, regex := range []string{fmt.Sprintf("%vfw-.*%v.*", k8sPrefix, clusterDelimiter), fmt.Sprintf("%vfws-.*%v.*", k8sPrefix, clusterDelimiter)} {
  136. gcloudComputeResourceList("forwarding-rules", regex, cont.Cloud.ProjectID, &fwList)
  137. if len(fwList) == 0 {
  138. continue
  139. }
  140. for _, f := range fwList {
  141. if !cont.canDelete(f.Name, f.CreationTimestamp, del) {
  142. continue
  143. }
  144. if del {
  145. GcloudComputeResourceDelete("forwarding-rules", f.Name, cont.Cloud.ProjectID, "--global")
  146. } else {
  147. msg += fmt.Sprintf("%v (forwarding rule)\n", f.Name)
  148. }
  149. }
  150. }
  151. return msg
  152. }
  153. // GetGlobalAddress returns the global address by name.
  154. func (cont *IngressController) GetGlobalAddress(ipName string) *compute.Address {
  155. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  156. ip, err := gceCloud.GetGlobalAddress(ipName)
  157. framework.ExpectNoError(err)
  158. return ip
  159. }
  160. func (cont *IngressController) deleteAddresses(del bool) string {
  161. msg := ""
  162. ipList := []compute.Address{}
  163. regex := fmt.Sprintf("%vfw-.*%v.*", k8sPrefix, clusterDelimiter)
  164. gcloudComputeResourceList("addresses", regex, cont.Cloud.ProjectID, &ipList)
  165. if len(ipList) != 0 {
  166. for _, ip := range ipList {
  167. if !cont.canDelete(ip.Name, ip.CreationTimestamp, del) {
  168. continue
  169. }
  170. if del {
  171. GcloudComputeResourceDelete("addresses", ip.Name, cont.Cloud.ProjectID, "--global")
  172. } else {
  173. msg += fmt.Sprintf("%v (static-ip)\n", ip.Name)
  174. }
  175. }
  176. }
  177. return msg
  178. }
  179. // ListTargetHTTPProxies lists all target HTTP proxies in the project
  180. func (cont *IngressController) ListTargetHTTPProxies() []*compute.TargetHttpProxy {
  181. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  182. tpList := []*compute.TargetHttpProxy{}
  183. l, err := gceCloud.ListTargetHTTPProxies()
  184. framework.ExpectNoError(err)
  185. for _, tp := range l {
  186. if cont.isOwned(tp.Name) {
  187. tpList = append(tpList, tp)
  188. }
  189. }
  190. return tpList
  191. }
  192. // ListTargetHTTPSProxies lists all target HTTPS proxies
  193. func (cont *IngressController) ListTargetHTTPSProxies() []*compute.TargetHttpsProxy {
  194. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  195. tpsList := []*compute.TargetHttpsProxy{}
  196. l, err := gceCloud.ListTargetHTTPSProxies()
  197. framework.ExpectNoError(err)
  198. for _, tps := range l {
  199. if cont.isOwned(tps.Name) {
  200. tpsList = append(tpsList, tps)
  201. }
  202. }
  203. return tpsList
  204. }
  205. func (cont *IngressController) deleteTargetProxy(del bool) string {
  206. msg := ""
  207. tpList := []compute.TargetHttpProxy{}
  208. regex := fmt.Sprintf("%vtp-.*%v.*", k8sPrefix, clusterDelimiter)
  209. gcloudComputeResourceList("target-http-proxies", regex, cont.Cloud.ProjectID, &tpList)
  210. if len(tpList) != 0 {
  211. for _, t := range tpList {
  212. if !cont.canDelete(t.Name, t.CreationTimestamp, del) {
  213. continue
  214. }
  215. if del {
  216. GcloudComputeResourceDelete("target-http-proxies", t.Name, cont.Cloud.ProjectID)
  217. } else {
  218. msg += fmt.Sprintf("%v (target-http-proxy)\n", t.Name)
  219. }
  220. }
  221. }
  222. tpsList := []compute.TargetHttpsProxy{}
  223. regex = fmt.Sprintf("%vtps-.*%v.*", k8sPrefix, clusterDelimiter)
  224. gcloudComputeResourceList("target-https-proxies", regex, cont.Cloud.ProjectID, &tpsList)
  225. if len(tpsList) != 0 {
  226. for _, t := range tpsList {
  227. if !cont.canDelete(t.Name, t.CreationTimestamp, del) {
  228. continue
  229. }
  230. if del {
  231. GcloudComputeResourceDelete("target-https-proxies", t.Name, cont.Cloud.ProjectID)
  232. } else {
  233. msg += fmt.Sprintf("%v (target-https-proxy)\n", t.Name)
  234. }
  235. }
  236. }
  237. return msg
  238. }
  239. // ListURLMaps lists all URL maps
  240. func (cont *IngressController) ListURLMaps() []*compute.UrlMap {
  241. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  242. umList := []*compute.UrlMap{}
  243. l, err := gceCloud.ListURLMaps()
  244. framework.ExpectNoError(err)
  245. for _, um := range l {
  246. if cont.isOwned(um.Name) {
  247. umList = append(umList, um)
  248. }
  249. }
  250. return umList
  251. }
  252. func (cont *IngressController) deleteURLMap(del bool) (msg string) {
  253. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  254. umList, err := gceCloud.ListURLMaps()
  255. if err != nil {
  256. if cont.isHTTPErrorCode(err, http.StatusNotFound) {
  257. return msg
  258. }
  259. return fmt.Sprintf("Failed to list url maps: %v", err)
  260. }
  261. if len(umList) == 0 {
  262. return msg
  263. }
  264. for _, um := range umList {
  265. if !cont.canDelete(um.Name, um.CreationTimestamp, del) {
  266. continue
  267. }
  268. if del {
  269. framework.Logf("Deleting url-map: %s", um.Name)
  270. if err := gceCloud.DeleteURLMap(um.Name); err != nil &&
  271. !cont.isHTTPErrorCode(err, http.StatusNotFound) {
  272. msg += fmt.Sprintf("Failed to delete url map %v\n", um.Name)
  273. }
  274. } else {
  275. msg += fmt.Sprintf("%v (url-map)\n", um.Name)
  276. }
  277. }
  278. return msg
  279. }
  280. // ListGlobalBackendServices lists all global backend services
  281. func (cont *IngressController) ListGlobalBackendServices() []*compute.BackendService {
  282. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  283. beList := []*compute.BackendService{}
  284. l, err := gceCloud.ListGlobalBackendServices()
  285. framework.ExpectNoError(err)
  286. for _, be := range l {
  287. if cont.isOwned(be.Name) {
  288. beList = append(beList, be)
  289. }
  290. }
  291. return beList
  292. }
  293. func (cont *IngressController) deleteBackendService(del bool) (msg string) {
  294. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  295. beList, err := gceCloud.ListGlobalBackendServices()
  296. if err != nil {
  297. if cont.isHTTPErrorCode(err, http.StatusNotFound) {
  298. return msg
  299. }
  300. return fmt.Sprintf("Failed to list backend services: %v", err)
  301. }
  302. if len(beList) == 0 {
  303. framework.Logf("No backend services found")
  304. return msg
  305. }
  306. for _, be := range beList {
  307. if !cont.canDelete(be.Name, be.CreationTimestamp, del) {
  308. continue
  309. }
  310. if del {
  311. framework.Logf("Deleting backed-service: %s", be.Name)
  312. if err := gceCloud.DeleteGlobalBackendService(be.Name); err != nil &&
  313. !cont.isHTTPErrorCode(err, http.StatusNotFound) {
  314. msg += fmt.Sprintf("Failed to delete backend service %v: %v\n", be.Name, err)
  315. }
  316. } else {
  317. msg += fmt.Sprintf("%v (backend-service)\n", be.Name)
  318. }
  319. }
  320. return msg
  321. }
  322. func (cont *IngressController) deleteHTTPHealthCheck(del bool) (msg string) {
  323. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  324. hcList, err := gceCloud.ListHTTPHealthChecks()
  325. if err != nil {
  326. if cont.isHTTPErrorCode(err, http.StatusNotFound) {
  327. return msg
  328. }
  329. return fmt.Sprintf("Failed to list HTTP health checks: %v", err)
  330. }
  331. if len(hcList) == 0 {
  332. return msg
  333. }
  334. for _, hc := range hcList {
  335. if !cont.canDelete(hc.Name, hc.CreationTimestamp, del) {
  336. continue
  337. }
  338. if del {
  339. framework.Logf("Deleting http-health-check: %s", hc.Name)
  340. if err := gceCloud.DeleteHTTPHealthCheck(hc.Name); err != nil &&
  341. !cont.isHTTPErrorCode(err, http.StatusNotFound) {
  342. msg += fmt.Sprintf("Failed to delete HTTP health check %v\n", hc.Name)
  343. }
  344. } else {
  345. msg += fmt.Sprintf("%v (http-health-check)\n", hc.Name)
  346. }
  347. }
  348. return msg
  349. }
  350. // ListSslCertificates lists all SSL certificates
  351. func (cont *IngressController) ListSslCertificates() []*compute.SslCertificate {
  352. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  353. sslList := []*compute.SslCertificate{}
  354. l, err := gceCloud.ListSslCertificates()
  355. framework.ExpectNoError(err)
  356. for _, ssl := range l {
  357. if cont.isOwned(ssl.Name) {
  358. sslList = append(sslList, ssl)
  359. }
  360. }
  361. return sslList
  362. }
  363. func (cont *IngressController) deleteSSLCertificate(del bool) (msg string) {
  364. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  365. sslList, err := gceCloud.ListSslCertificates()
  366. if err != nil {
  367. if cont.isHTTPErrorCode(err, http.StatusNotFound) {
  368. return msg
  369. }
  370. return fmt.Sprintf("Failed to list ssl certificates: %v", err)
  371. }
  372. if len(sslList) != 0 {
  373. for _, s := range sslList {
  374. if !cont.canDelete(s.Name, s.CreationTimestamp, del) {
  375. continue
  376. }
  377. if del {
  378. framework.Logf("Deleting ssl-certificate: %s", s.Name)
  379. if err := gceCloud.DeleteSslCertificate(s.Name); err != nil &&
  380. !cont.isHTTPErrorCode(err, http.StatusNotFound) {
  381. msg += fmt.Sprintf("Failed to delete ssl certificates: %v\n", s.Name)
  382. }
  383. } else {
  384. msg += fmt.Sprintf("%v (ssl-certificate)\n", s.Name)
  385. }
  386. }
  387. }
  388. return msg
  389. }
  390. // ListInstanceGroups lists all instance groups
  391. func (cont *IngressController) ListInstanceGroups() []*compute.InstanceGroup {
  392. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  393. igList := []*compute.InstanceGroup{}
  394. l, err := gceCloud.ListInstanceGroups(cont.Cloud.Zone)
  395. framework.ExpectNoError(err)
  396. for _, ig := range l {
  397. if cont.isOwned(ig.Name) {
  398. igList = append(igList, ig)
  399. }
  400. }
  401. return igList
  402. }
  403. func (cont *IngressController) deleteInstanceGroup(del bool) (msg string) {
  404. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  405. // TODO: E2E cloudprovider has only 1 zone, but the cluster can have many.
  406. // We need to poll on all IGs across all zones.
  407. igList, err := gceCloud.ListInstanceGroups(cont.Cloud.Zone)
  408. if err != nil {
  409. if cont.isHTTPErrorCode(err, http.StatusNotFound) {
  410. return msg
  411. }
  412. return fmt.Sprintf("Failed to list instance groups: %v", err)
  413. }
  414. if len(igList) == 0 {
  415. return msg
  416. }
  417. for _, ig := range igList {
  418. if !cont.canDelete(ig.Name, ig.CreationTimestamp, del) {
  419. continue
  420. }
  421. if del {
  422. framework.Logf("Deleting instance-group: %s", ig.Name)
  423. if err := gceCloud.DeleteInstanceGroup(ig.Name, cont.Cloud.Zone); err != nil &&
  424. !cont.isHTTPErrorCode(err, http.StatusNotFound) {
  425. msg += fmt.Sprintf("Failed to delete instance group %v\n", ig.Name)
  426. }
  427. } else {
  428. msg += fmt.Sprintf("%v (instance-group)\n", ig.Name)
  429. }
  430. }
  431. return msg
  432. }
  433. func (cont *IngressController) deleteNetworkEndpointGroup(del bool) (msg string) {
  434. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  435. // TODO: E2E cloudprovider has only 1 zone, but the cluster can have many.
  436. // We need to poll on all NEGs across all zones.
  437. negList, err := gceCloud.ListNetworkEndpointGroup(cont.Cloud.Zone)
  438. if err != nil {
  439. if cont.isHTTPErrorCode(err, http.StatusNotFound) {
  440. return msg
  441. }
  442. // Do not return error as NEG is still alpha.
  443. framework.Logf("Failed to list network endpoint group: %v", err)
  444. return msg
  445. }
  446. if len(negList) == 0 {
  447. return msg
  448. }
  449. for _, neg := range negList {
  450. if !cont.canDeleteNEG(neg.Name, neg.CreationTimestamp, del) {
  451. continue
  452. }
  453. if del {
  454. framework.Logf("Deleting network-endpoint-group: %s", neg.Name)
  455. if err := gceCloud.DeleteNetworkEndpointGroup(neg.Name, cont.Cloud.Zone); err != nil &&
  456. !cont.isHTTPErrorCode(err, http.StatusNotFound) {
  457. msg += fmt.Sprintf("Failed to delete network endpoint group %v\n", neg.Name)
  458. }
  459. } else {
  460. msg += fmt.Sprintf("%v (network-endpoint-group)\n", neg.Name)
  461. }
  462. }
  463. return msg
  464. }
  465. // canDelete returns true if either the name ends in a suffix matching this
  466. // controller's UID, or the creationTimestamp exceeds the maxAge and del is set
  467. // to true. Always returns false if the name doesn't match that we expect for
  468. // Ingress cloud resources.
  469. func (cont *IngressController) canDelete(resourceName, creationTimestamp string, delOldResources bool) bool {
  470. // ignore everything not created by an ingress controller.
  471. splitName := strings.Split(resourceName, clusterDelimiter)
  472. if !strings.HasPrefix(resourceName, k8sPrefix) || len(splitName) != 2 {
  473. return false
  474. }
  475. // Resources created by the GLBC have a "0"" appended to the end if truncation
  476. // occurred. Removing the zero allows the following match.
  477. truncatedClusterUID := splitName[1]
  478. if len(truncatedClusterUID) >= 1 && strings.HasSuffix(truncatedClusterUID, "0") {
  479. truncatedClusterUID = truncatedClusterUID[:len(truncatedClusterUID)-1]
  480. }
  481. // always delete things that are created by the current ingress controller.
  482. // Because of resource name truncation, this looks for a common prefix
  483. if strings.HasPrefix(cont.UID, truncatedClusterUID) {
  484. return true
  485. }
  486. if !delOldResources {
  487. return false
  488. }
  489. return canDeleteWithTimestamp(resourceName, creationTimestamp)
  490. }
  491. // isOwned returns true if the resourceName ends in a suffix matching this
  492. // controller UID.
  493. func (cont *IngressController) isOwned(resourceName string) bool {
  494. return cont.canDelete(resourceName, "", false)
  495. }
  496. // canDeleteNEG returns true if either the name contains this controller's UID,
  497. // or the creationTimestamp exceeds the maxAge and del is set to true.
  498. func (cont *IngressController) canDeleteNEG(resourceName, creationTimestamp string, delOldResources bool) bool {
  499. if !strings.HasPrefix(resourceName, "k8s") {
  500. return false
  501. }
  502. if strings.Contains(resourceName, cont.UID) {
  503. return true
  504. }
  505. if !delOldResources {
  506. return false
  507. }
  508. return canDeleteWithTimestamp(resourceName, creationTimestamp)
  509. }
  510. func canDeleteWithTimestamp(resourceName, creationTimestamp string) bool {
  511. createdTime, err := time.Parse(time.RFC3339, creationTimestamp)
  512. if err != nil {
  513. framework.Logf("WARNING: Failed to parse creation timestamp %v for %v: %v", creationTimestamp, resourceName, err)
  514. return false
  515. }
  516. if time.Since(createdTime) > maxAge {
  517. framework.Logf("%v created on %v IS too old", resourceName, creationTimestamp)
  518. return true
  519. }
  520. return false
  521. }
  522. // GetFirewallRuleName returns the name of the firewall used for the IngressController.
  523. func (cont *IngressController) GetFirewallRuleName() string {
  524. return fmt.Sprintf("%vfw-l7%v%v", k8sPrefix, clusterDelimiter, cont.UID)
  525. }
  526. // GetFirewallRule returns the firewall used by the IngressController.
  527. // Returns an error if that fails.
  528. func (cont *IngressController) GetFirewallRule() (*compute.Firewall, error) {
  529. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  530. fwName := cont.GetFirewallRuleName()
  531. return gceCloud.GetFirewall(fwName)
  532. }
  533. func (cont *IngressController) deleteFirewallRule(del bool) (msg string) {
  534. fwList := []compute.Firewall{}
  535. regex := fmt.Sprintf("%vfw-l7%v.*", k8sPrefix, clusterDelimiter)
  536. gcloudComputeResourceList("firewall-rules", regex, cont.Cloud.ProjectID, &fwList)
  537. if len(fwList) != 0 {
  538. for _, f := range fwList {
  539. if !cont.canDelete(f.Name, f.CreationTimestamp, del) {
  540. continue
  541. }
  542. if del {
  543. GcloudComputeResourceDelete("firewall-rules", f.Name, cont.Cloud.ProjectID)
  544. } else {
  545. msg += fmt.Sprintf("%v (firewall rule)\n", f.Name)
  546. }
  547. }
  548. }
  549. return msg
  550. }
  551. func (cont *IngressController) isHTTPErrorCode(err error, code int) bool {
  552. apiErr, ok := err.(*googleapi.Error)
  553. return ok && apiErr.Code == code
  554. }
  555. // WaitForNegBackendService waits for the expected backend service to become
  556. func (cont *IngressController) WaitForNegBackendService(svcPorts map[string]v1.ServicePort) error {
  557. return wait.Poll(5*time.Second, 1*time.Minute, func() (bool, error) {
  558. err := cont.verifyBackendMode(svcPorts, negBackend)
  559. if err != nil {
  560. framework.Logf("Err while checking if backend service is using NEG: %v", err)
  561. return false, nil
  562. }
  563. return true, nil
  564. })
  565. }
  566. // WaitForIgBackendService returns true only if all global backend service with matching svcPorts pointing to IG as backend
  567. func (cont *IngressController) WaitForIgBackendService(svcPorts map[string]v1.ServicePort) error {
  568. return wait.Poll(5*time.Second, 1*time.Minute, func() (bool, error) {
  569. err := cont.verifyBackendMode(svcPorts, igBackend)
  570. if err != nil {
  571. framework.Logf("Err while checking if backend service is using IG: %v", err)
  572. return false, nil
  573. }
  574. return true, nil
  575. })
  576. }
  577. // BackendServiceUsingNEG returns true only if all global backend service with matching svcPorts pointing to NEG as backend
  578. func (cont *IngressController) BackendServiceUsingNEG(svcPorts map[string]v1.ServicePort) error {
  579. return cont.verifyBackendMode(svcPorts, negBackend)
  580. }
  581. // BackendServiceUsingIG returns true only if all global backend service with matching svcPorts pointing to IG as backend
  582. func (cont *IngressController) BackendServiceUsingIG(svcPorts map[string]v1.ServicePort) error {
  583. return cont.verifyBackendMode(svcPorts, igBackend)
  584. }
  585. func (cont *IngressController) verifyBackendMode(svcPorts map[string]v1.ServicePort, backendType backendType) error {
  586. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  587. beList, err := gceCloud.ListGlobalBackendServices()
  588. if err != nil {
  589. return fmt.Errorf("failed to list backend services: %v", err)
  590. }
  591. hcList, err := gceCloud.ListHealthChecks()
  592. if err != nil {
  593. return fmt.Errorf("failed to list health checks: %v", err)
  594. }
  595. // Generate short UID
  596. uid := cont.UID
  597. if len(uid) > 8 {
  598. uid = uid[:8]
  599. }
  600. matchingBackendService := 0
  601. for svcName, sp := range svcPorts {
  602. match := false
  603. bsMatch := &compute.BackendService{}
  604. // NEG BackendServices' names contain the a sha256 hash of a string.
  605. // This logic is copied from the ingress-gce namer.
  606. // WARNING: This needs to adapt if the naming convention changed.
  607. negString := strings.Join([]string{uid, cont.Ns, svcName, fmt.Sprintf("%v", sp.Port)}, ";")
  608. negHash := fmt.Sprintf("%x", sha256.Sum256([]byte(negString)))[:8]
  609. for _, bs := range beList {
  610. // Non-NEG BackendServices are named with the Nodeport in the name.
  611. if backendType == igBackend && strings.Contains(bs.Name, strconv.Itoa(int(sp.NodePort))) {
  612. match = true
  613. bsMatch = bs
  614. matchingBackendService++
  615. break
  616. }
  617. // NEG BackendServices' names contain the a sha256 hash of a string.
  618. if backendType == negBackend && strings.Contains(bs.Name, negHash) {
  619. match = true
  620. bsMatch = bs
  621. matchingBackendService++
  622. break
  623. }
  624. }
  625. if match {
  626. for _, be := range bsMatch.Backends {
  627. if !strings.Contains(be.Group, string(backendType)) {
  628. return fmt.Errorf("expect to find backends with type %q, but got backend group: %v", backendType, be.Group)
  629. }
  630. }
  631. // Check that the correct HealthCheck exists for the BackendService
  632. hcMatch := false
  633. for _, hc := range hcList {
  634. if hc.Name == bsMatch.Name {
  635. hcMatch = true
  636. break
  637. }
  638. }
  639. if !hcMatch {
  640. return fmt.Errorf("missing healthcheck for backendservice: %v", bsMatch.Name)
  641. }
  642. }
  643. }
  644. if matchingBackendService != len(svcPorts) {
  645. beNames := []string{}
  646. for _, be := range beList {
  647. beNames = append(beNames, be.Name)
  648. }
  649. return fmt.Errorf("expect %d backend service with backend type: %v, but got %d matching backend service. Expect backend services for service ports: %v, but got backend services: %v", len(svcPorts), backendType, matchingBackendService, svcPorts, beNames)
  650. }
  651. return nil
  652. }
  653. // Cleanup cleans up cloud resources.
  654. // If del is false, it simply reports existing resources without deleting them.
  655. // If dle is true, it deletes resources it finds acceptable (see canDelete func).
  656. func (cont *IngressController) Cleanup(del bool) error {
  657. // Ordering is important here because we cannot delete resources that other
  658. // resources hold references to.
  659. errMsg := cont.deleteForwardingRule(del)
  660. // Static IPs are named after forwarding rules.
  661. errMsg += cont.deleteAddresses(del)
  662. errMsg += cont.deleteTargetProxy(del)
  663. errMsg += cont.deleteURLMap(del)
  664. errMsg += cont.deleteBackendService(del)
  665. errMsg += cont.deleteHTTPHealthCheck(del)
  666. errMsg += cont.deleteInstanceGroup(del)
  667. errMsg += cont.deleteNetworkEndpointGroup(del)
  668. errMsg += cont.deleteFirewallRule(del)
  669. errMsg += cont.deleteSSLCertificate(del)
  670. // TODO: Verify instance-groups, issue #16636. Gcloud mysteriously barfs when told
  671. // to unmarshal instance groups into the current vendored gce-client's understanding
  672. // of the struct.
  673. if errMsg == "" {
  674. return nil
  675. }
  676. return fmt.Errorf(errMsg)
  677. }
  678. // Init initializes the IngressController with an UID
  679. func (cont *IngressController) Init() error {
  680. uid, err := cont.getL7AddonUID()
  681. if err != nil {
  682. return err
  683. }
  684. cont.UID = uid
  685. // There's a name limit imposed by GCE. The controller will truncate.
  686. testName := fmt.Sprintf("k8s-fw-foo-app-X-%v--%v", cont.Ns, cont.UID)
  687. if len(testName) > nameLenLimit {
  688. framework.Logf("WARNING: test name including cluster UID: %v is over the GCE limit of %v", testName, nameLenLimit)
  689. } else {
  690. framework.Logf("Detected cluster UID %v", cont.UID)
  691. }
  692. return nil
  693. }
  694. // CreateStaticIP allocates a random static ip with the given name. Returns a string
  695. // representation of the ip. Caller is expected to manage cleanup of the ip by
  696. // invoking deleteStaticIPs.
  697. func (cont *IngressController) CreateStaticIP(name string) string {
  698. gceCloud := cont.Cloud.Provider.(*Provider).gceCloud
  699. addr := &compute.Address{Name: name}
  700. if err := gceCloud.ReserveGlobalAddress(addr); err != nil {
  701. if delErr := gceCloud.DeleteGlobalAddress(name); delErr != nil {
  702. if cont.isHTTPErrorCode(delErr, http.StatusNotFound) {
  703. framework.Logf("Static ip with name %v was not allocated, nothing to delete", name)
  704. } else {
  705. framework.Logf("Failed to delete static ip %v: %v", name, delErr)
  706. }
  707. }
  708. framework.Failf("Failed to allocate static ip %v: %v", name, err)
  709. }
  710. ip, err := gceCloud.GetGlobalAddress(name)
  711. if err != nil {
  712. framework.Failf("Failed to get newly created static ip %v: %v", name, err)
  713. }
  714. cont.staticIPName = ip.Name
  715. framework.Logf("Reserved static ip %v: %v", cont.staticIPName, ip.Address)
  716. return ip.Address
  717. }
  718. // deleteStaticIPs delets all static-ips allocated through calls to
  719. // CreateStaticIP.
  720. func (cont *IngressController) deleteStaticIPs() error {
  721. if cont.staticIPName != "" {
  722. if err := GcloudComputeResourceDelete("addresses", cont.staticIPName, cont.Cloud.ProjectID, "--global"); err == nil {
  723. cont.staticIPName = ""
  724. } else {
  725. return err
  726. }
  727. } else {
  728. e2eIPs := []compute.Address{}
  729. gcloudComputeResourceList("addresses", "e2e-.*", cont.Cloud.ProjectID, &e2eIPs)
  730. ips := []string{}
  731. for _, ip := range e2eIPs {
  732. ips = append(ips, ip.Name)
  733. }
  734. framework.Logf("None of the remaining %d static-ips were created by this e2e: %v", len(ips), strings.Join(ips, ", "))
  735. }
  736. return nil
  737. }
  738. // gcloudComputeResourceList unmarshals json output of gcloud into given out interface.
  739. func gcloudComputeResourceList(resource, regex, project string, out interface{}) {
  740. // gcloud prints a message to stderr if it has an available update
  741. // so we only look at stdout.
  742. command := []string{
  743. "compute", resource, "list",
  744. fmt.Sprintf("--filter='name ~ \"%q\"'", regex),
  745. fmt.Sprintf("--project=%v", project),
  746. "-q", "--format=json",
  747. }
  748. output, err := exec.Command("gcloud", command...).Output()
  749. if err != nil {
  750. errCode := -1
  751. errMsg := ""
  752. if exitErr, ok := err.(utilexec.ExitError); ok {
  753. errCode = exitErr.ExitStatus()
  754. errMsg = exitErr.Error()
  755. if osExitErr, ok := err.(*exec.ExitError); ok {
  756. errMsg = fmt.Sprintf("%v, stderr %v", errMsg, string(osExitErr.Stderr))
  757. }
  758. }
  759. framework.Logf("Error running gcloud command 'gcloud %s': err: %v, output: %v, status: %d, msg: %v", strings.Join(command, " "), err, string(output), errCode, errMsg)
  760. }
  761. if err := json.Unmarshal([]byte(output), out); err != nil {
  762. framework.Logf("Error unmarshalling gcloud output for %v: %v, output: %v", resource, err, string(output))
  763. }
  764. }
  765. // GcloudComputeResourceDelete deletes the specified compute resource by name and project.
  766. func GcloudComputeResourceDelete(resource, name, project string, args ...string) error {
  767. framework.Logf("Deleting %v: %v", resource, name)
  768. argList := append([]string{"compute", resource, "delete", name, fmt.Sprintf("--project=%v", project), "-q"}, args...)
  769. output, err := exec.Command("gcloud", argList...).CombinedOutput()
  770. if err != nil {
  771. framework.Logf("Error deleting %v, output: %v\nerror: %+v", resource, string(output), err)
  772. }
  773. return err
  774. }
  775. // GcloudComputeResourceCreate creates a compute resource with a name and arguments.
  776. func GcloudComputeResourceCreate(resource, name, project string, args ...string) error {
  777. framework.Logf("Creating %v in project %v: %v", resource, project, name)
  778. argsList := append([]string{"compute", resource, "create", name, fmt.Sprintf("--project=%v", project)}, args...)
  779. framework.Logf("Running command: gcloud %+v", strings.Join(argsList, " "))
  780. output, err := exec.Command("gcloud", argsList...).CombinedOutput()
  781. if err != nil {
  782. framework.Logf("Error creating %v, output: %v\nerror: %+v", resource, string(output), err)
  783. }
  784. return err
  785. }