Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #210 from StrongMonkey/more-fix3
Browse files Browse the repository at this point in the history
fix router and autoscaling
  • Loading branch information
ibuildthecloud authored May 19, 2019
2 parents 18e676d + 267867c commit bc2e16b
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 135 deletions.
11 changes: 8 additions & 3 deletions cli/cmd/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/rancher/rio/cli/pkg/clicontext"
"github.com/rancher/rio/pkg/version"
"github.com/urfave/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Info(app *cli.App) cli.Command {
Expand All @@ -20,14 +20,19 @@ func Info(app *cli.App) cli.Command {
func info(ctx *clicontext.CLIContext) error {
builder := &strings.Builder{}

info, err := ctx.Project.RioInfos().Get("rio", metav1.GetOptions{})
if err != nil {
return err
}

domain, err := ctx.Domain()
if err != nil {
return err
}

builder.WriteString(fmt.Sprintf("Rio CLI Version: %s\n", version.Version))
builder.WriteString(fmt.Sprintf("Rio CLI Version: %s (%s)\n", info.Status.Version, info.Status.GitCommit))
builder.WriteString(fmt.Sprintf("Cluster Domain: %s\n", domain))
builder.WriteString(fmt.Sprintf("System Namespace: %s", ctx.SystemNamespace))
builder.WriteString(fmt.Sprintf("System Namespace: %s", info.Status.SystemNamespace))
fmt.Println(builder.String())
return nil
}
3 changes: 3 additions & 0 deletions cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Install struct {
}

func (i *Install) Run(ctx *clicontext.CLIContext) error {
if ctx.K8s == nil {
return fmt.Errorf("can't contact Kubernetes cluster. Please make sure your cluster is accessable")
}
controllerStack := systemstack.NewStack(ctx.Apply, i.Namespace, "rio-controller", true)
if _, err := ctx.Core.Namespaces().Get(i.Namespace, metav1.GetOptions{}); err != nil {
if errors.IsNotFound(err) {
Expand Down
1 change: 0 additions & 1 deletion cli/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (r *Run) Run(ctx *clicontext.CLIContext) error {
fmt.Println("Attaching...")
return attach.RunAttach(ctx, time.Minute, true, true, service.Name)
}
fmt.Printf("%s/%s\n", service.Namespace, service.Name)

return nil
}
5 changes: 1 addition & 4 deletions cli/cmd/stage/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ func (r *Stage) Run(ctx *clicontext.CLIContext) error {
}

stagedService.Spec = *spec
if err := ctx.Create(stagedService); err != nil {
return err
}
fmt.Printf("%s/%s:%s\n", stagedService.Namespace, app.Name, version)
return ctx.Create(stagedService)
}
}

Expand Down
45 changes: 45 additions & 0 deletions cli/cmd/systemlogs/systemlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package systemlogs

import (
"bufio"
"fmt"

"github.com/docker/libcompose/cli/logger"
"github.com/rancher/rio/cli/pkg/clicontext"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type SystemLog struct {
}

func (s SystemLog) Run(ctx *clicontext.CLIContext) error {
pods, err := ctx.Core.Pods(ctx.SystemNamespace).List(metav1.ListOptions{
LabelSelector: "rio-controller=true",
})
if err != nil {
return err
}

if len(pods.Items) == 0 {
return fmt.Errorf("failed to find rio controller pod")
}

factory := logger.NewColorLoggerFactory()
logger := factory.CreateContainerLogger("rio-controller")
req := ctx.Core.Pods(pods.Items[0].Namespace).GetLogs(pods.Items[0].Name, &v1.PodLogOptions{
Follow: true,
})
reader, err := req.Stream()
if err != nil {
return err
}
defer reader.Close()

sc := bufio.NewScanner(reader)
for sc.Scan() {
logger.Out(append(sc.Bytes(), []byte("\n")...))
}

return nil
}
34 changes: 26 additions & 8 deletions cli/cmd/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"context"
"time"

"github.com/rancher/axe/throwing"
"github.com/rancher/rio/cli/pkg/clicontext"
Expand All @@ -14,6 +15,7 @@ import (
)

type Tui struct {
Refresh string `desc:"refresh based on polling or controller (polling|controller)" default:"polling"`
}

func (t *Tui) Run(ctx *clicontext.CLIContext) error {
Expand All @@ -31,14 +33,30 @@ func (t *Tui) Run(ctx *clicontext.CLIContext) error {
signals: ss,
}

rioContext := types.NewContext(ctx.SystemNamespace, ctx.RestConfig)
go func() {
leader.RunOrDie(ctx.Ctx, ctx.SystemNamespace, "rio-cli", rioContext.K8s, func(context context.Context) {
register(context, rioContext, h)
runtime2.Must(rioContext.Start(context))
<-context.Done()
})
}()
if t.Refresh == "controller" {
rioContext := types.NewContext(ctx.SystemNamespace, ctx.RestConfig)
go func() {
leader.RunOrDie(ctx.Ctx, ctx.SystemNamespace, "rio-cli", rioContext.K8s, func(context context.Context) {
register(context, rioContext, h)
runtime2.Must(rioContext.Start(context))
<-context.Done()
})
}()
} else {
go func() {
for {
select {
case <-time.Tick(time.Second * 2):
for _, s := range ss {
signal := s
go func() {
signal <- struct{}{}
}()
}
}
}
}()
}

tui := throwing.NewAppView(ctx.K8s, drawer, tableEventHandler, ss)
if err := tui.Init(); err != nil {
Expand Down
23 changes: 19 additions & 4 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

"github.com/rancher/rio/cli/cmd/systemlogs"

"github.com/docker/docker/pkg/reexec"
"github.com/rancher/rio/cli/cmd/apply"
"github.com/rancher/rio/cli/cmd/attach"
Expand Down Expand Up @@ -215,11 +217,17 @@ func main() {
"Terminal interactive UI",
" tui",
""),
builder.Command(&systemlogs.SystemLog{},
"View system log for Rio management plane",
" systemlog",
""),
route.Route(app),
}
app.Before = func(ctx *cli.Context) error {
if err := cfg.Validate(); err != nil {
return err
if len(ctx.Args()) > 0 && ctx.Args()[0] != "install" {
return err
}
}
cc := clicontext.CLIContext{
Config: &cfg,
Expand All @@ -244,11 +252,18 @@ func main() {

func printConfigUsage() {
fmt.Print(`
No configuration found to contact server. If you already have a Rio cluster running then run
No configuration found to contact server. If you already have a Kubernetes cluster running then you should point your Kubernetes cluster using
export KUBECONFIG=/path/to/config
If you don't have rio installed then you should run the following command to install Rio into your current cluster.
rio install
rio login
If you don't have an existing server you should run "rio server" on a Linux server.
If you are just looking for general "rio" CLI usage then run
rio --help
Expand Down
6 changes: 5 additions & 1 deletion cli/pkg/clicontext/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Config) Validate() error {

restConfig, err := loader.ClientConfig()
if err != nil {
return err
return ErrNoConfig
}

project, err := projectv1.NewForConfig(restConfig)
Expand Down Expand Up @@ -95,6 +95,10 @@ func (c *Config) Validate() error {
c.Project = project
c.Core = core
c.Build = build

if _, err := project.RioInfos().Get("rio", metav1.GetOptions{}); err != nil {
return ErrNoConfig
}
return nil
}

Expand Down
14 changes: 13 additions & 1 deletion cli/pkg/clicontext/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"time"

"github.com/rancher/rio/pkg/services"

"github.com/docker/docker/pkg/namesgenerator"
"github.com/rancher/rio/cli/pkg/lookup"
"github.com/rancher/rio/cli/pkg/types"
Expand Down Expand Up @@ -149,6 +151,12 @@ func (c *CLIContext) Create(obj runtime.Object) (err error) {
switch o := obj.(type) {
case *riov1.Service:
_, err = c.Rio.Services(o.Namespace).Create(o)
if err != nil {
return err
}
app, version := services.AppAndVersion(o)
fmt.Printf("%s/%s:%s\n", o.Namespace, app, version)
return nil
case *corev1.Pod:
_, err = c.Core.Pods(o.Namespace).Create(o)
case *corev1.ConfigMap:
Expand All @@ -162,7 +170,11 @@ func (c *CLIContext) Create(obj runtime.Object) (err error) {
default:
return fmt.Errorf("unknown delete type %v", reflect.TypeOf(obj))
}
return
if err != nil {
return err
}
fmt.Printf("%s/%s\n", metadata.GetNamespace(), metadata.GetName())
return nil
}

func (c *CLIContext) UpdateObject(obj runtime.Object) (err error) {
Expand Down
18 changes: 10 additions & 8 deletions modules/autoscale/controller/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

autoscalev1 "github.com/rancher/rio/pkg/apis/autoscale.rio.cattle.io/v1"
v1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
autoscalev1controller "github.com/rancher/rio/pkg/generated/controllers/autoscale.rio.cattle.io/v1"
riov1controller "github.com/rancher/rio/pkg/generated/controllers/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/services"
"github.com/rancher/rio/pkg/stackobject"
"github.com/rancher/rio/types"
"github.com/rancher/wrangler/pkg/objectset"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
Expand Down Expand Up @@ -75,26 +75,28 @@ func (p populator) populateServiceRecommendation(object runtime.Object, ns *core

type handler struct {
services riov1controller.ServiceController
ssrs autoscalev1controller.ServiceScaleRecommendationController
}

func (h handler) sync(key string, obj *autoscalev1.ServiceScaleRecommendation) (*autoscalev1.ServiceScaleRecommendation, error) {
if obj == nil || obj.DeletionTimestamp != nil {
return obj, nil
}
return obj, setServiceScale(h.services, obj)
return obj, h.setServiceScale(obj)
}

func setServiceScale(rioServices riov1controller.ServiceController, ssr *autoscalev1.ServiceScaleRecommendation) error {
svc, err := rioServices.Cache().Get(ssr.Namespace, ssr.Name)
func (h handler) setServiceScale(ssr *autoscalev1.ServiceScaleRecommendation) error {
svc, err := h.services.Cache().Get(ssr.Namespace, ssr.Name)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
// wait for a minute after scale from zero
if svc.Status.ScaleFromZeroTimestamp != nil && svc.Status.ScaleFromZeroTimestamp.Add(time.Minute).After(time.Now()) {
logrus.Infof("skipping setting scale because service %s/%s is scaled from zero within a minute", svc.Namespace, svc.Name)
go func() {
time.Sleep(time.Second * 60)
h.ssrs.Enqueue(ssr.Namespace, ssr.Name)
}()
return nil
}

Expand All @@ -108,7 +110,7 @@ func setServiceScale(rioServices riov1controller.ServiceController, ssr *autosca
logrus.Infof("Setting desired scale %v for %v/%v", *ssr.Status.DesiredScale, svc.Namespace, svc.Name)

svc.Status.ObservedScale = &observedScale
if _, err := rioServices.Update(svc); err != nil {
if _, err := h.services.Update(svc); err != nil {
return err
}
return nil
Expand Down
16 changes: 9 additions & 7 deletions modules/build/controllers/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Register(ctx context.Context, rContext *types.Context) error {

p := populator{
systemNamespace: rContext.Namespace,
appCache: rContext.Rio.Rio().V1().App().Cache(),
secretsCache: rContext.Core.Core().V1().Secret().Cache(),
clusterDomainCache: rContext.Global.Admin().V1().ClusterDomain().Cache(),
serviceCache: rContext.Rio.Rio().V1().Service().Cache(),
Expand All @@ -56,6 +57,7 @@ func Register(ctx context.Context, rContext *types.Context) error {
type populator struct {
systemNamespace string
customRegistry string
appCache v1.AppCache
secretsCache corev1controller.SecretCache
serviceCache v1.ServiceCache
clusterDomainCache projectv1controller.ClusterDomainCache
Expand Down Expand Up @@ -107,7 +109,7 @@ func (p *populator) populate(obj runtime.Object, ns *corev1.Namespace, os *objec
return err
}

webhook, err := p.serviceCache.Get(p.systemNamespace, "webhook")
webhook, err := p.appCache.Get(p.systemNamespace, "webhook")
if errors.IsNotFound(err) {
webhook = nil
} else if err != nil {
Expand Down Expand Up @@ -163,14 +165,14 @@ func populateBuild(service *riov1.Service, customRegistry, systemNamespace, doma
return nil
}

func populateWebhookAndSecrets(webhookService *riov1.Service, service *riov1.Service, os *objectset.ObjectSet) {
func populateWebhookAndSecrets(webhookService *riov1.App, service *riov1.Service, os *objectset.ObjectSet) {
webhookReceiver := webhookv1.NewGitWatcher(service.Namespace, service.Name, webhookv1.GitWatcher{
Spec: webhookv1.GitWatcherSpec{
RepositoryURL: service.Spec.Build.Repo,
Enabled: true,
Push: true,
Tag: true,
Branch: service.Spec.Build.Branch,
RepositoryURL: service.Spec.Build.Repo,
Enabled: true,
Push: true,
Tag: true,
Branch: service.Spec.Build.Branch,
RepositoryCredentialSecretName: service.Spec.Build.Secret,
},
})
Expand Down
3 changes: 1 addition & 2 deletions modules/istio/controllers/istio/populate/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"strconv"
"strings"

"github.com/rancher/rio/pkg/apis/admin.rio.cattle.io/v1"

"github.com/knative/pkg/apis/istio/v1alpha3"
"github.com/rancher/rio/modules/system/features/letsencrypt/pkg/issuers"
v1 "github.com/rancher/rio/pkg/apis/admin.rio.cattle.io/v1"
"github.com/rancher/rio/pkg/constants"
"github.com/rancher/rio/pkg/constructors"
"github.com/rancher/wrangler/pkg/objectset"
Expand Down
Loading

0 comments on commit bc2e16b

Please sign in to comment.