Skip to content

Commit

Permalink
Merge pull request hashicorp#11 from sl1pm4t/apps-v1
Browse files Browse the repository at this point in the history
Support Kubernetes v1.9.0 and apps/v1 API
  • Loading branch information
sl1pm4t authored May 1, 2018
2 parents e8fc10c + fe5384d commit 9ba31d7
Show file tree
Hide file tree
Showing 1,190 changed files with 232,334 additions and 325,939 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ website/vendor
scripts/runtests*.sh

dist/*
build/*
build/*
terraform-provider-kubernetes
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ BUG FIXES:
* resource/kubernetes_persistent_volume: Update `persistent_volume_reclaim_policy` correctly [GH-111]
* resource/kubernetes_service: Update external_ips correctly on K8S 1.8+ [GH-127]
* resource/kubernetes_*: Fix adding labels/annotations to resources when those were empty [GH-116]
* resource/kubernetes_*: Treat non-string label values as invalid [GH-135]
* resource/kubernetes_config_map: Fix adding `data` when it was empty [GH-116]
* resource/kubernetes_secret: Fix adding `data` when it was empty [GH-116]
* resource/kubernetes_limit_range: Avoid spurious diff when spec is empty [GH-132]
* resource/kubernetes_persistent_volume: Use correct operation when updating `persistent_volume_source` (`1.8`) [GH-133]
* resource/kubernetes_persistent_volume: Mark persistent_volume_source as ForceNew on `1.9+` [GH-139]
* resource/kubernetes_pod: Bump deletion timeout to 5 mins [GH-136]

## 1.0.1 (November 13, 2017)

Expand Down
130 changes: 130 additions & 0 deletions kubernetes/api_versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package kubernetes

import (
"encoding/json"
"log"
"strconv"

"time"

"fmt"

"k8s.io/client-go/kubernetes"
)

type APIGroup int

const (
none APIGroup = iota
appsV1
appsV1beta1
appsV1beta2
extensionsV1beta1
)

func (g APIGroup) String() string {
switch g {
case appsV1:
return "apps/v1"
case appsV1beta1:
return "apps/v1beta1"
case appsV1beta2:
return "apps/v1beta2"
case extensionsV1beta1:
return "extensions/v1beta1"
default:
return "none"
}
}

func (kp *kubernetesProvider) lowestSupportedAPIGroup(rtype string, groups ...APIGroup) (APIGroup, error) {
for i := len(groups) - 1; i > -1; i-- {
g := groups[i]
match, err := kp.serverSupportsResourceAPIVersion(rtype, g.String())
if err != nil {
return none, err
} else if match {
return g, nil
}
}
return none, nil
}

func (kp *kubernetesProvider) highestSupportedAPIGroup(rtype string, groups ...APIGroup) (APIGroup, error) {
for _, g := range groups {
match, err := kp.serverSupportsResourceAPIVersion(rtype, g.String())
if err != nil {
return none, err
} else if match {
return g, nil
}
}
return none, nil
}

func (kp *kubernetesProvider) serverSupportsResourceAPIVersion(rname string, groupVersion string) (bool, error) {
start := time.Now()
resList, err := kp.discoClient.ServerResources()
//resList, err := providerInstance.conn.DiscoveryClient.ServerResources()
if err != nil {
log.Printf("[WARN] discovery client could not resource list: %v\n", err)
return false, err
}
log.Printf("[DEBUG] retrieved resource list in %v\n", time.Now().Sub(start))

for _, v := range resList {
if v.GroupVersion == groupVersion {
for _, v2 := range v.APIResources {
if v2.Name == rname {
log.Printf("[DEBUG] api group [%s] supports %s resource type\n", groupVersion, rname)
return true, nil
}
}
}
}
log.Printf("[DEBUG] api group [%s] does not supports %s resource type on Kubernetes server\n", groupVersion, rname)

return false, nil
}

// Convert between two types by converting to/from JSON. Intended to switch
// between multiple API versions, as they are strict supersets of one another.
// item and out are pointers to structs
func Convert(item, out interface{}) error {
bytes, err := json.Marshal(item)
if err != nil {
return err
}

err = json.Unmarshal(bytes, out)
if err != nil {
return err
}

return nil
}

// ServerVersionPre1_9 reads the Kubernetes API verions and returns true if less
// than v1.9
func (kp *kubernetesProvider) ServerVersionPre1_9(conn *kubernetes.Clientset) bool {
ver, _ := kp.discoClient.ServerVersion()
minor, _ := strconv.Atoi(string(ver.Minor[0]))
log.Printf("[INFO] Kubernetes Server version: %#v", ver)

if ver.Major == "1" && minor < 9 {
return true
}

return false
}

func printObjectJSON(item interface{}) error {
bytes, err := json.MarshalIndent(item, "", " ")
if err != nil {
return err
}

fmt.Println(string(bytes))

return nil
}
Loading

0 comments on commit 9ba31d7

Please sign in to comment.