forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f65c81
commit d503a73
Showing
4 changed files
with
97 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package knative | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"github.com/boson-project/faas" | ||
"github.com/boson-project/faas/k8s" | ||
"io" | ||
commands "knative.dev/client/pkg/kn/commands" | ||
"knative.dev/client/pkg/kn/core" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func NewDeployer() *Deployer { | ||
return &Deployer{Namespace: faas.DefaultNamespace} | ||
} | ||
|
||
type Deployer struct { | ||
Namespace string | ||
Verbose bool | ||
} | ||
|
||
func (deployer *Deployer) Deploy(name, image string) (address string, err error) { | ||
|
||
project, err := k8s.ToSubdomain(name) | ||
if err != nil { | ||
return | ||
} | ||
nn := strings.Split(name, ".") | ||
if len(nn) < 3 { | ||
err = errors.New(fmt.Sprintf("invalid service name '%v', must be at least three parts.\n", name)) | ||
} | ||
|
||
subDomain := nn[0] | ||
domain := strings.Join(nn[1:], ".") | ||
|
||
output := io.Writer(nil) | ||
if deployer.Verbose { | ||
output = os.Stdout | ||
} else { | ||
output = &bytes.Buffer{} | ||
} | ||
|
||
params := commands.KnParams{} | ||
params.Initialize() | ||
params.Output = output | ||
c := core.NewKnCommand(params) | ||
c.SetOut(output) | ||
args := []string{ | ||
"service", "create", project, | ||
"--image", image, | ||
"--namespace", deployer.Namespace, | ||
"--env", "VERBOSE=true", | ||
"--label", fmt.Sprintf("faas.domain=%s", domain), | ||
"--label", "bosonFunction=true", | ||
"--annotation", fmt.Sprintf("faas.subdomain=%s", subDomain), | ||
} | ||
c.SetArgs(args) | ||
err = c.Execute() | ||
if err != nil { | ||
if !deployer.Verbose { | ||
err = fmt.Errorf("failed to deploy the service: %v.\nStdOut: %s", err, output.(*bytes.Buffer).String()) | ||
} else { | ||
err = fmt.Errorf("failed to deploy the service: %v", err) | ||
} | ||
return | ||
} | ||
|
||
return "[cluster-address]", nil | ||
} |