Skip to content

Commit

Permalink
Merge pull request #8 from mgfeller/feature/mgfeller/7
Browse files Browse the repository at this point in the history
Add function GetServicePorts() to return port(s) of a named k8s-service
  • Loading branch information
kumarabd authored Oct 19, 2020
2 parents ff725d0 + 029c2c8 commit a48de88
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
15 changes: 12 additions & 3 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func (h *BaseHandler) CreateInstance(kubeconfig []byte, contextName string, ch *
h.Channel = ch
h.KubeConfigPath = h.Config.GetKey("kube-config-path")

config, err := h.k8sClientConfig(kubeconfig, contextName)
k8sConfig, err := h.k8sClientConfig(kubeconfig, contextName)
if err != nil {
return ErrClientConfig(err)
}

clientset, err := kubernetes.NewForConfig(config)
clientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return ErrClientSet(err)
}

h.KubeClient = clientset

dynamicClient, err := dynamic.NewForConfig(config)
dynamicClient, err := dynamic.NewForConfig(k8sConfig)
if err != nil {
return err
}
Expand All @@ -93,6 +93,15 @@ func (h *BaseHandler) CreateNamespace(isDelete bool, namespace string) error {
return nil
}

func (h *BaseHandler) GetServicePorts(serviceName, namespace string) ([]int64, error) {
ports, err := h.getServicePorts(context.TODO(), serviceName, namespace)
if err != nil {
logrus.Error(err)
return nil, err
}
return ports, nil
}

func (h *BaseHandler) ApplyKubernetesManifest(request OperationRequest, operation Operation, mergeData map[string]string, templatePath string) error {
if err := h.applyK8sManifest(context.TODO(), request, operation, mergeData, templatePath); err != nil {
logrus.Error(err)
Expand Down
30 changes: 30 additions & 0 deletions adapter/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,33 @@ func (h *BaseHandler) applyK8sManifest(ctx context.Context, request OperationReq

return nil
}

func (h *BaseHandler) getServicePorts(ctx context.Context, svc, namespace string) ([]int64, error) {
ns := &unstructured.Unstructured{}
res := schema.GroupVersionResource{
Version: "v1",
Resource: "services",
}
ns.SetName(svc)
ns.SetNamespace(namespace)
ns, err := h.getResource(ctx, res, ns)
if err != nil {
err = gherrors.Wrapf(err, "unable to get service details")
logrus.Error(err)
return nil, err
}
svcInst := ns.UnstructuredContent()
spec := svcInst["spec"].(map[string]interface{})
ports, _ := spec["ports"].([]interface{})
nodePorts := []int64{}
for _, port := range ports {
p, _ := port.(map[string]interface{})
np, ok := p["nodePort"]
if ok {
npi, _ := np.(int64)
nodePorts = append(nodePorts, npi)
}
}
logrus.Debugf("retrieved svc: %+#v", ns)
return nodePorts, nil
}

0 comments on commit a48de88

Please sign in to comment.