Skip to content

Commit

Permalink
Allow to define api rules as part of function manifest (kyma-project#838
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pPrecel authored Apr 17, 2021
1 parent 2719426 commit 5fd09d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
40 changes: 39 additions & 1 deletion cmd/kyma/apply/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/kyma-incubator/hydroform/function/pkg/client"
"github.com/kyma-incubator/hydroform/function/pkg/manager"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -67,12 +69,15 @@ func (c *command) Run() error {
}

// Load project configuration
step := c.NewStep("Loading configuration...")
var configuration workspace.Cfg
if err := yaml.NewDecoder(file).Decode(&configuration); err != nil {
step.Failure()
return errors.Wrap(err, "Could not decode the configuration file")
}

if c.K8s, err = kube.NewFromConfig("", c.KubeconfigPath); err != nil {
step.Failure()
return errors.Wrap(err, "Could not initialize the Kubernetes client. Make sure your kubeconfig is valid")
}
client := c.K8s.Dynamic()
Expand All @@ -81,27 +86,43 @@ func (c *command) Run() error {

function, err := resources.NewFunction(configuration)
if err != nil {
step.Failure()
return err
}

subscriptions, err := resources.NewSubscriptions(configuration)
if err != nil {
step.Failure()
return err
}

kymaAddress, err := c.kymaHostAddress()
if err != nil {
step.LogErrorf("%s\n%s", err, "Check if your cluster is available and has Kyma installed.")
}

apiRules, err := resources.NewAPIRule(configuration, kymaAddress)
if err != nil {
step.Failure()
return err
}

if configuration.Source.Type == workspace.SourceTypeGit {
gitRepository, err := resources.NewPublicGitRepository(configuration)
if err != nil {
step.Failure()
return errors.Wrap(err, "Unable to read the Git repository from the provided configuration")
}
mgr.AddParent(operator.NewGenericOperator(client.Resource(operator.GVRGitRepository).Namespace(configuration.Namespace), gitRepository), nil)
}

mgr.AddParent(
operator.NewGenericOperator(client.Resource(operator.GVKFunction).Namespace(configuration.Namespace), function),
operator.NewGenericOperator(client.Resource(operator.GVRFunction).Namespace(configuration.Namespace), function),
[]operator.Operator{
operator.NewSubscriptionOperator(client.Resource(operator.GVRSubscription).Namespace(configuration.Namespace),
configuration.Name, configuration.Namespace, subscriptions...),
operator.NewAPIRuleOperator(client.Resource(operator.GVRApiRule).Namespace(configuration.Namespace),
configuration.Name, apiRules...),
},
)

Expand All @@ -119,9 +140,26 @@ func (c *command) Run() error {
}
defer cancel()

step.Successf("Configuration loaded")

return mgr.Do(ctx, options)
}

func (c *command) kymaHostAddress() (string, error) {
var url string
vs, err := c.K8s.Istio().NetworkingV1alpha3().Gateways("kyma-system").Get(context.Background(), "kyma-gateway", v1.GetOptions{})
switch {
case err != nil:
err = errors.Wrapf(err, "Unable to read the Kyma host URL due to error")
case vs != nil && len(vs.Spec.GetServers()) > 0 && len(vs.Spec.Servers[0].Hosts) > 0:
url = strings.TrimPrefix(vs.Spec.Servers[0].Hosts[0], "*.")
default:
err = errors.New("kyma host URL could not be obtained")
}

return url, err
}

const (
operatingFormat = "%s - %s operating... %s"
createdFormat = "%s - %s created %s"
Expand Down
6 changes: 5 additions & 1 deletion cmd/kyma/init/function/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func (o *Options) setDefaults(defaultNamespace string) (err error) {
}

if o.Name == "" {
o.Name = "function-" + generator.GenerateName(true)
generated, err := generator.GenerateName(true)
if err != nil {
return err
}
o.Name = "function-" + generated
}

if o.RepositoryName == "" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/docker/cli v20.10.3+incompatible
github.com/docker/docker v20.10.3+incompatible
github.com/fatih/color v1.10.0
github.com/kyma-incubator/hydroform/function v0.0.0-20210319130226-926c73801013
github.com/kyma-incubator/hydroform/function v0.0.0-20210412105828-8e9bd1971490
github.com/kyma-incubator/hydroform/install v0.0.0-20200922142757-cae045912c90
github.com/kyma-incubator/hydroform/parallel-install v0.0.0-20210409084448-8e90ab5f2803
github.com/kyma-incubator/hydroform/provision v0.0.0-20201124135641-ca1a1a00c935
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,8 @@ github.com/kubernetes-sigs/service-catalog v0.3.0/go.mod h1:zRfgMd1T9HuXR24Qj4GO
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/kyma-incubator/hydroform/function v0.0.0-20210319130226-926c73801013 h1:tWDA411I0kzyOHfMqKX988pSApaJcmk8DuAHD6cBiGs=
github.com/kyma-incubator/hydroform/function v0.0.0-20210319130226-926c73801013/go.mod h1:UkGyJJw4UKHUAbgKcJWDYQrMw76k5DJUZDNkOvjawqA=
github.com/kyma-incubator/hydroform/function v0.0.0-20210412105828-8e9bd1971490 h1:YZoVabHqKr/Hxuvcae5EfD7p7j3Tl4O57jLBk2pJXrU=
github.com/kyma-incubator/hydroform/function v0.0.0-20210412105828-8e9bd1971490/go.mod h1:UkGyJJw4UKHUAbgKcJWDYQrMw76k5DJUZDNkOvjawqA=
github.com/kyma-incubator/hydroform/install v0.0.0-20200922142757-cae045912c90 h1:E9rWdmz/CEfO52VJ6P/1M3TysNbET/qyw6611RbKJo8=
github.com/kyma-incubator/hydroform/install v0.0.0-20200922142757-cae045912c90/go.mod h1:/qouJL+g8Tsllh/VcxK1Li6NCyuqyXSlq1i9InKSZJk=
github.com/kyma-incubator/hydroform/parallel-install v0.0.0-20210409084448-8e90ab5f2803 h1:1WLaXk65JTSeegOtZi9nwpUWnL6tbcSufaN5AGOY8ho=
Expand Down

0 comments on commit 5fd09d6

Please sign in to comment.