-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add dettached mode Signed-off-by: Javier López Barba <javier@okteto.com> * fix: tests Signed-off-by: Javier López Barba <javier@okteto.com> * fix: address comments Signed-off-by: Javier López Barba <javier@okteto.com> * fix: unit tests Signed-off-by: Javier López Barba <javier@okteto.com>
- Loading branch information
1 parent
8c0ee5c
commit b1653c0
Showing
15 changed files
with
936 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2022 The Okteto Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package up | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"regexp" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/okteto/okteto/pkg/config" | ||
oktetoErrors "github.com/okteto/okteto/pkg/errors" | ||
oktetoLog "github.com/okteto/okteto/pkg/log" | ||
"github.com/okteto/okteto/pkg/okteto" | ||
"github.com/pkg/errors" | ||
"github.com/stern/stern/stern" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
var tempKubeConfigTemplate = "%s/.okteto/kubeconfig-%s" | ||
|
||
func (up *upContext) showDetachedLogs(ctx context.Context) error { | ||
tmpKubeconfig, err := createTempKubeconfig(up.Manifest.Name) | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(tmpKubeconfig) | ||
|
||
c, err := getSternConfig(tmpKubeconfig) | ||
if err != nil { | ||
return err | ||
} | ||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt) | ||
exit := make(chan error, 1) | ||
go func() { | ||
if err := stern.Run(ctx, c); err != nil { | ||
exit <- oktetoErrors.UserError{ | ||
E: fmt.Errorf("failed to get logs: %w", err), | ||
} | ||
} | ||
}() | ||
select { | ||
case <-stop: | ||
oktetoLog.Infof("CTRL+C received, starting shutdown sequence") | ||
return oktetoErrors.ErrIntSig | ||
case err := <-exit: | ||
if err != nil { | ||
oktetoLog.Infof("exit signal received due to error: %s", err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func createTempKubeconfig(name string) (string, error) { | ||
cfg := okteto.Context().Cfg | ||
destKubeconfigFile := fmt.Sprintf(tempKubeConfigTemplate, config.GetUserHomeDir(), name) | ||
if err := clientcmd.WriteToFile(*cfg, destKubeconfigFile); err != nil { | ||
oktetoLog.Errorf("could not modify the k8s config: %s", err) | ||
return "", err | ||
} | ||
return destKubeconfigFile, nil | ||
} | ||
|
||
func getSternConfig(kubeconfigPath string) (*stern.Config, error) { | ||
labelSelector, err := labels.Parse("detached.dev.okteto.com") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse selector as label selector: %s", err.Error()) | ||
} | ||
pod, err := regexp.Compile("") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to compile regular expression from query: %w", err) | ||
} | ||
container, err := regexp.Compile(".*") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to compile regular expression for container query") | ||
} | ||
var tailLines *int64 | ||
|
||
funs := template.FuncMap{ | ||
"json": func(in interface{}) (string, error) { | ||
b, err := json.Marshal(in) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(b), nil | ||
}, | ||
"parseJSON": func(text string) (map[string]interface{}, error) { | ||
obj := make(map[string]interface{}) | ||
if err := json.Unmarshal([]byte(text), &obj); err != nil { | ||
return obj, err | ||
} | ||
return obj, nil | ||
}, | ||
"color": func(color color.Color, text string) string { | ||
return color.SprintFunc()(text) | ||
}, | ||
} | ||
t := "{{color .PodColor .PodName}} {{color .ContainerColor .ContainerName}} {{.Message}}\n" | ||
|
||
tmpl, err := template.New("logs").Funcs(funs).Parse(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &stern.Config{ | ||
KubeConfig: kubeconfigPath, | ||
ContextName: okteto.UrlToKubernetesContext(okteto.Context().Name), | ||
Namespaces: []string{okteto.Context().Namespace}, | ||
PodQuery: pod, | ||
ContainerQuery: container, | ||
InitContainers: true, | ||
EphemeralContainers: true, | ||
TailLines: tailLines, | ||
Since: 48 * time.Hour, | ||
LabelSelector: labelSelector, | ||
FieldSelector: fields.Everything(), | ||
AllNamespaces: false, | ||
ErrOut: os.Stderr, | ||
Out: os.Stdout, | ||
ContainerStates: []stern.ContainerState{"running"}, | ||
Template: tmpl, | ||
}, nil | ||
} |
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
Oops, something went wrong.