Skip to content

Commit

Permalink
Merge pull request #13 from containerum/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MargoTuleninova committed Aug 17, 2018
2 parents 9c4daa9 + 096860b commit 735ab6e
Show file tree
Hide file tree
Showing 128 changed files with 3,888 additions and 930 deletions.
257 changes: 213 additions & 44 deletions Gopkg.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gopkg.toml
Expand Up @@ -71,7 +71,7 @@ required = ["github.com/containerum/kube-client"]

[[constraint]]
name = "github.com/containerum/kube-client"
version = "^0.23.20"
version = "^0.23.23"

[[constraint]]
name = "k8s.io/kubernetes"
Expand Down
10 changes: 8 additions & 2 deletions pkg/kubeErrors/Errors.toml → pkg/kubeerrors/Errors.toml
@@ -1,6 +1,6 @@
# File for noice errors generation

Name = "kubeErrors"
Name = "kubeerrors"
SID = "Kube-API"

# errors
Expand Down Expand Up @@ -102,4 +102,10 @@ SID = "Kube-API"
Name = "ErrVolumeNotReady"
StatusHTTP = 503
Message = "Volume is not ready"
Kind = 16
Kind = 16

[[error]]
Name = "ErrUnableDownsizeVolume"
StatusHTTP = 400
Message = "Unable to downsize volume"
Kind = 17
2 changes: 1 addition & 1 deletion pkg/kubeErrors/errors.go → pkg/kubeerrors/errors.go
@@ -1,3 +1,3 @@
package kubeErrors
package kubeerrors

//go:generate noice -t Errors.toml -o .
14 changes: 13 additions & 1 deletion pkg/kubeErrors/kubeErrors.go → pkg/kubeerrors/kubeerrors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/kubernetes/pod.go
Expand Up @@ -4,7 +4,7 @@ import (
"io"
"net/http"

"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -14,10 +14,10 @@ import (

//TODO: Imp struct to GetPodLogs func
type LogOptions struct {
Follow bool
Container string
Tail int64
Follow bool
Previous bool
Container string
}

type ExecOptions struct {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (k *Kube) Exec(ns string, po string, opt *ExecOptions) error {
}

if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
return kubeErrors.ErrRequestValidationFailed().
return kubeerrors.ErrRequestValidationFailed().
AddDetailF("cannot exec into a container in a completed pod; current phase is %s", pod.Status.Phase)
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/kubernetes/storage.go
@@ -0,0 +1,16 @@
package kubernetes

import (
log "github.com/sirupsen/logrus"
api_storage "k8s.io/api/storage/v1"
api_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (k *Kube) GetStorageClassesList() (*api_storage.StorageClassList, error) {
storages, err := k.StorageV1().StorageClasses().List(api_meta.ListOptions{})
if err != nil {
log.Error(err)
return nil, err
}
return storages, nil
}
1 change: 1 addition & 0 deletions pkg/model/configmap.go
Expand Up @@ -52,6 +52,7 @@ func ParseKubeConfigMap(cmi interface{}, parseforuser bool) (*kube_types.ConfigM

newCm := kube_types.ConfigMap{
Name: cm.GetName(),
Namespace: cm.Namespace,
CreatedAt: cm.CreationTimestamp.UTC().Format(time.RFC3339),
Data: kube_types.ConfigMapData(newData),
Owner: owner,
Expand Down
9 changes: 5 additions & 4 deletions pkg/model/deployment.go
Expand Up @@ -10,7 +10,7 @@ import (

"time"

"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
"github.com/blang/semver"
kube_types "github.com/containerum/kube-client/pkg/model"
api_apps "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -75,8 +75,9 @@ func ParseKubeDeployment(deployment interface{}, parseforuser bool) (*kube_types
version, _ := semver.ParseTolerant(deploy.GetObjectMeta().GetLabels()[versionLabel])

newDeploy := kube_types.Deployment{
Name: deploy.GetName(),
Replicas: replicas,
Name: deploy.GetName(),
Namespace: deploy.Namespace,
Replicas: replicas,
Status: &kube_types.DeploymentStatus{
Replicas: int(deploy.Status.Replicas),
ReadyReplicas: int(deploy.Status.ReadyReplicas),
Expand Down Expand Up @@ -144,7 +145,7 @@ func (deploy *DeploymentKubeAPI) ToKube(nsName string, labels map[string]string)
}

if labels == nil {
return nil, []error{kubeErrors.ErrInternalError().AddDetails("invalid project labels")}
return nil, []error{kubeerrors.ErrInternalError().AddDetails("invalid project labels")}
}

labels[appLabel] = deploy.Name
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/endpoint.go
Expand Up @@ -6,7 +6,7 @@ import (

"time"

"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
kube_types "github.com/containerum/kube-client/pkg/model"
api_core "k8s.io/api/core/v1"
api_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -121,7 +121,7 @@ func (endpoint *Endpoint) ToKube(nsName string, labels map[string]string) (*api_
}

if labels == nil {
return nil, []error{kubeErrors.ErrInternalError().AddDetails("invalid project labels")}
return nil, []error{kubeerrors.ErrInternalError().AddDetails("invalid project labels")}
}

newEndpoint := api_core.Endpoints{
Expand Down
10 changes: 6 additions & 4 deletions pkg/model/errors.go
Expand Up @@ -5,7 +5,7 @@ import (

"fmt"

"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
"github.com/containerum/cherry"
api_errors "k8s.io/apimachinery/pkg/api/errors"
api_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -38,6 +38,8 @@ var (

ErrUnableConvertConfigMapList = errors.New("unable to decode config maps list")
ErrUnableConvertConfigMap = errors.New("unable to decode config map")

ErrUnableConvertStorageList = errors.New("unable to decode storage class list")
)

const (
Expand Down Expand Up @@ -65,11 +67,11 @@ func ParseKubernetesResourceError(in interface{}, defaultErr *cherry.Err) *cherr
switch sE.ErrStatus.Reason {
case api_meta.StatusReasonNotFound:
if sE.Status().Details.Kind == "resourcequotas" {
return kubeErrors.ErrResourceNotExist().AddDetails(noNamespace)
return kubeerrors.ErrResourceNotExist().AddDetails(noNamespace)
}
return kubeErrors.ErrResourceNotExist().AddDetailsErr(fmt.Errorf(noResource, sE.Status().Details.Name, sE.Status().Details.Kind))
return kubeerrors.ErrResourceNotExist().AddDetailsErr(fmt.Errorf(noResource, sE.Status().Details.Name, sE.Status().Details.Kind))
case api_meta.StatusReasonAlreadyExists:
return kubeErrors.ErrResourceAlreadyExists().AddDetailsErr(fmt.Errorf(resourceAlreadyExists, sE.Status().Details.Name, sE.Status().Details.Kind))
return kubeerrors.ErrResourceAlreadyExists().AddDetailsErr(fmt.Errorf(resourceAlreadyExists, sE.Status().Details.Name, sE.Status().Details.Kind))
default:
return defaultErr
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/model/ingress.go
@@ -1,7 +1,7 @@
package model

import (
"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
kube_types "github.com/containerum/kube-client/pkg/model"
api_extensions "k8s.io/api/extensions/v1beta1"

Expand Down Expand Up @@ -48,6 +48,7 @@ func ParseKubeIngress(ingressi interface{}, parseforuser bool) (*kube_types.Ingr

newIngress := kube_types.Ingress{
Name: ingress.GetName(),
Namespace: ingress.Namespace,
CreatedAt: ingress.CreationTimestamp.UTC().Format(time.RFC3339),
Rules: parseRules(ingress.Spec.Rules, parseTLS(ingress.Spec.TLS)),
Owner: ingress.GetObjectMeta().GetLabels()[ownerLabel],
Expand Down Expand Up @@ -102,7 +103,7 @@ func (ingress *IngressKubeAPI) ToKube(nsName string, labels map[string]string) (
return nil, err
}
if labels == nil {
return nil, []error{kubeErrors.ErrInternalError().AddDetails("invalid project labels")}
return nil, []error{kubeerrors.ErrInternalError().AddDetails("invalid project labels")}
}

rules, secrets, tls := makeIngressRules(ingress.Rules)
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/namespace.go
Expand Up @@ -27,7 +27,7 @@ type NamespaceKubeAPI kube_types.Namespace

// ParseKubeResourceQuotaList parses kubernetes v1.ResourceQuotaList to more convenient []Namespace struct.
// (resource quouta contains all fields that parent namespace contains)
func ParseKubeResourceQuotaList(quotas interface{}, parseforuser bool) (*kube_types.NamespacesList, error) {
func ParseKubeResourceQuotaList(quotas interface{}) (*kube_types.NamespacesList, error) {
objects := quotas.(*api_core.ResourceQuotaList)
if objects == nil {
return nil, ErrUnableConvertNamespaceList
Expand Down
7 changes: 4 additions & 3 deletions pkg/model/secret.go
Expand Up @@ -6,7 +6,7 @@ import (

"time"

"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
kube_types "github.com/containerum/kube-client/pkg/model"
api_core "k8s.io/api/core/v1"
api_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -52,6 +52,7 @@ func ParseKubeSecret(secreti interface{}, parseforuser bool) (*kube_types.Secret

newSecret := kube_types.Secret{
Name: secret.GetName(),
Namespace: secret.Namespace,
CreatedAt: secret.CreationTimestamp.UTC().Format(time.RFC3339),
Data: newData,
Owner: secret.GetObjectMeta().GetLabels()[ownerLabel],
Expand All @@ -73,11 +74,11 @@ func (secret *SecretKubeAPI) ToKube(nsName string, labels map[string]string, sec
}

if labels == nil {
return nil, []error{kubeErrors.ErrInternalError().AddDetails("invalid project labels")}
return nil, []error{kubeerrors.ErrInternalError().AddDetails("invalid project labels")}
}

if secretType == api_core.SecretTypeDockerConfigJson && secret.Data[".dockerconfigjson"] == "" {
return nil, []error{kubeErrors.ErrRequestValidationFailed().AddDetails("field '.dockerconfigjson' is required")}
return nil, []error{kubeerrors.ErrRequestValidationFailed().AddDetails("field '.dockerconfigjson' is required")}
}

newSecret := api_core.Secret{
Expand Down
5 changes: 3 additions & 2 deletions pkg/model/service.go
@@ -1,7 +1,7 @@
package model

import (
"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
kube_types "github.com/containerum/kube-client/pkg/model"
api_core "k8s.io/api/core/v1"
api_meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -80,6 +80,7 @@ func ParseKubeService(srv interface{}, parseforuser bool) (*ServiceWithParam, er
service := ServiceWithParam{
Service: &kube_types.Service{
Name: native.Name,
Namespace: native.Namespace,
CreatedAt: native.GetCreationTimestamp().UTC().Format(time.RFC3339),
Ports: ports,
Deploy: native.GetObjectMeta().GetLabels()[appLabel],
Expand Down Expand Up @@ -129,7 +130,7 @@ func (service *ServiceWithParam) ToKube(nsName string, labels map[string]string)
}

if labels == nil {
return nil, []error{kubeErrors.ErrInternalError().AddDetails("invalid project labels")}
return nil, []error{kubeerrors.ErrInternalError().AddDetails("invalid project labels")}
}
labels[appLabel] = service.Deploy
labels[hiddenLabel] = strconv.FormatBool(service.Hidden)
Expand Down
24 changes: 24 additions & 0 deletions pkg/model/storages.go
@@ -0,0 +1,24 @@
package model

import (
api_storage "k8s.io/api/storage/v1"
)

// StorageList -- model for storage names list
//
// swagger:model
type StorageList []string

// ParseStoragesList parses kubernetes v1.StorageClassList to more convenient StorageList struct.
func ParseStoragesList(storages interface{}) (*StorageList, error) {
nativeStorages := storages.(*api_storage.StorageClassList)
if nativeStorages == nil {
return nil, ErrUnableConvertStorageList
}

storageList := make(StorageList, len(nativeStorages.Items))
for i := range nativeStorages.Items {
storageList[i] = nativeStorages.Items[i].Name
}
return &storageList, nil
}
23 changes: 20 additions & 3 deletions pkg/model/volume.go
Expand Up @@ -7,7 +7,7 @@ import (

"strings"

"git.containerum.net/ch/kube-api/pkg/kubeErrors"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
kube_types "github.com/containerum/kube-client/pkg/model"
api_core "k8s.io/api/core/v1"
api_resource "k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -50,8 +50,9 @@ func ParseKubePersistentVolumeClaim(pvci interface{}, parseforuser bool) (*kube_
capacity := native.Spec.Resources.Requests["storage"]

pvc := kube_types.Volume{
Status: string(native.Status.Phase),
Name: native.Name,
Namespace: native.Namespace,
Status: string(native.Status.Phase),
CreatedAt: native.GetCreationTimestamp().UTC().Format(time.RFC3339),
StorageName: native.ObjectMeta.Annotations[api_core.BetaStorageClassAnnotation],
AccessMode: kube_types.PersistentVolumeAccessMode(native.Spec.AccessModes[0]),
Expand All @@ -76,7 +77,7 @@ func (pvc *VolumeKubeAPI) ToKube(nsName string, labels map[string]string) (*api_
}

if labels == nil {
return nil, []error{kubeErrors.ErrInternalError().AddDetails("invalid project labels")}
return nil, []error{kubeerrors.ErrInternalError().AddDetails("invalid project labels")}
}

memsize := api_resource.NewQuantity(int64(pvc.Capacity)*1024*1024*1024, api_resource.BinarySI)
Expand Down Expand Up @@ -105,6 +106,22 @@ func (pvc *VolumeKubeAPI) ToKube(nsName string, labels map[string]string) (*api_
return &newPvc, nil
}

// ToKube creates kubernetes v1.Service from Service struct and namespace labels
func (pvc *VolumeKubeAPI) Resize(oldpvc *api_core.PersistentVolumeClaim) (*api_core.PersistentVolumeClaim, error) {
if oldpvc.Status.Phase != api_core.ClaimBound {
return nil, kubeerrors.ErrVolumeNotReady()
}

memsize := api_resource.NewQuantity(int64(pvc.Capacity)*1024*1024*1024, api_resource.BinarySI)
if memsize.Cmp(oldpvc.Spec.Resources.Requests["storage"]) < 1 {
return nil, kubeerrors.ErrUnableDownsizeVolume()
}

oldpvc.Spec.Resources.Requests["storage"] = *memsize

return oldpvc, nil
}

func (pvc *VolumeKubeAPI) Validate() []error {
var errs []error
if pvc.Name == "" {
Expand Down

0 comments on commit 735ab6e

Please sign in to comment.