ingress.go 29 KB

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