mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-25 07:45:01 +00:00
Merge pull request #66 from up9inc/mizu_view
TRA-3235 Implementation of Mizu view command
This commit is contained in:
commit
ba7b97cf7b
@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,7 +8,7 @@ var viewCmd = &cobra.Command{
|
|||||||
Use: "view",
|
Use: "view",
|
||||||
Short: "Open GUI in browser",
|
Short: "Open GUI in browser",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
fmt.Println("Not implemented")
|
runMizuView()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
33
cli/cmd/viewRunner.go
Normal file
33
cli/cmd/viewRunner.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func runMizuView() {
|
||||||
|
kubernetesProvider := kubernetes.NewProvider("", "")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
exists, err := kubernetesProvider.DoesServicesExist(ctx, mizu.ResourcesNamespace, mizu.AggregatorPodName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
fmt.Printf("The %s service not found\n", mizu.AggregatorPodName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = http.Get("http://localhost:8899/")
|
||||||
|
if err == nil {
|
||||||
|
fmt.Printf("Found a running service %s and open port 8899\n", mizu.AggregatorPodName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("Found service %s, creating port forwarding to 8899\n", mizu.AggregatorPodName)
|
||||||
|
portForwardApiPod(ctx, kubernetesProvider, cancel, &MizuTapOptions{GuiPort: 8899, MizuPodPort: 8899})
|
||||||
|
}
|
@ -6,7 +6,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
@ -95,7 +94,7 @@ func (provider *Provider) CreateMizuAggregatorPod(ctx context.Context, namespace
|
|||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: podName,
|
Name: podName,
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Labels: map[string]string{"app": podName},
|
Labels: map[string]string{"app": podName},
|
||||||
},
|
},
|
||||||
Spec: core.PodSpec{
|
Spec: core.PodSpec{
|
||||||
Containers: []core.Container{
|
Containers: []core.Container{
|
||||||
@ -103,20 +102,20 @@ func (provider *Provider) CreateMizuAggregatorPod(ctx context.Context, namespace
|
|||||||
Name: podName,
|
Name: podName,
|
||||||
Image: podImage,
|
Image: podImage,
|
||||||
ImagePullPolicy: core.PullAlways,
|
ImagePullPolicy: core.PullAlways,
|
||||||
Command: []string {"./mizuagent", "--aggregator"},
|
Command: []string{"./mizuagent", "--aggregator"},
|
||||||
Env: []core.EnvVar{
|
Env: []core.EnvVar{
|
||||||
{
|
{
|
||||||
Name: shared.HostModeEnvVar,
|
Name: shared.HostModeEnvVar,
|
||||||
Value: "1",
|
Value: "1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: shared.MizuFilteringOptionsEnvVar,
|
Name: shared.MizuFilteringOptionsEnvVar,
|
||||||
Value: string(marshaledFilteringOptions),
|
Value: string(marshaledFilteringOptions),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
DNSPolicy: core.DNSClusterFirstWithHostNet,
|
DNSPolicy: core.DNSClusterFirstWithHostNet,
|
||||||
TerminationGracePeriodSeconds: new(int64),
|
TerminationGracePeriodSeconds: new(int64),
|
||||||
// Affinity: TODO: define node selector for all relevant nodes for this mizu instance
|
// Affinity: TODO: define node selector for all relevant nodes for this mizu instance
|
||||||
},
|
},
|
||||||
@ -131,19 +130,19 @@ func (provider *Provider) CreateMizuAggregatorPod(ctx context.Context, namespace
|
|||||||
func (provider *Provider) CreateService(ctx context.Context, namespace string, serviceName string, appLabelValue string) (*core.Service, error) {
|
func (provider *Provider) CreateService(ctx context.Context, namespace string, serviceName string, appLabelValue string) (*core.Service, error) {
|
||||||
service := core.Service{
|
service := core.Service{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: serviceName,
|
Name: serviceName,
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
},
|
},
|
||||||
Spec: core.ServiceSpec{
|
Spec: core.ServiceSpec{
|
||||||
Ports: []core.ServicePort {{TargetPort: intstr.FromInt(8899), Port: 80}},
|
Ports: []core.ServicePort{{TargetPort: intstr.FromInt(8899), Port: 80}},
|
||||||
Type: core.ServiceTypeClusterIP,
|
Type: core.ServiceTypeClusterIP,
|
||||||
Selector: map[string]string{"app": appLabelValue},
|
Selector: map[string]string{"app": appLabelValue},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return provider.clientSet.CoreV1().Services(namespace).Create(ctx, &service, metav1.CreateOptions{})
|
return provider.clientSet.CoreV1().Services(namespace).Create(ctx, &service, metav1.CreateOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) DoesMizuRBACExist(ctx context.Context, namespace string) (bool, error){
|
func (provider *Provider) DoesMizuRBACExist(ctx context.Context, namespace string) (bool, error) {
|
||||||
serviceAccount, err := provider.clientSet.CoreV1().ServiceAccounts(namespace).Get(ctx, serviceAccountName, metav1.GetOptions{})
|
serviceAccount, err := provider.clientSet.CoreV1().ServiceAccounts(namespace).Get(ctx, serviceAccountName, metav1.GetOptions{})
|
||||||
|
|
||||||
var statusError *k8serrors.StatusError
|
var statusError *k8serrors.StatusError
|
||||||
@ -159,7 +158,22 @@ func (provider *Provider) DoesMizuRBACExist(ctx context.Context, namespace strin
|
|||||||
return serviceAccount != nil, nil
|
return serviceAccount != nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) CreateMizuRBAC(ctx context.Context, namespace string ,version string) error {
|
func (provider *Provider) DoesServicesExist(ctx context.Context, namespace string, serviceName string) (bool, error) {
|
||||||
|
service, err := provider.clientSet.CoreV1().Services(namespace).Get(ctx, serviceName, metav1.GetOptions{})
|
||||||
|
|
||||||
|
var statusError *k8serrors.StatusError
|
||||||
|
if errors.As(err, &statusError) {
|
||||||
|
if statusError.ErrStatus.Reason == metav1.StatusReasonNotFound {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return service != nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (provider *Provider) CreateMizuRBAC(ctx context.Context, namespace string, version string) error {
|
||||||
clusterRoleName := "mizu-cluster-role"
|
clusterRoleName := "mizu-cluster-role"
|
||||||
|
|
||||||
serviceAccount := &core.ServiceAccount{
|
serviceAccount := &core.ServiceAccount{
|
||||||
@ -171,25 +185,25 @@ func (provider *Provider) CreateMizuRBAC(ctx context.Context, namespace string ,
|
|||||||
}
|
}
|
||||||
clusterRole := &rbac.ClusterRole{
|
clusterRole := &rbac.ClusterRole{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: clusterRoleName,
|
Name: clusterRoleName,
|
||||||
Labels: map[string]string{"mizu-cli-version": version},
|
Labels: map[string]string{"mizu-cli-version": version},
|
||||||
},
|
},
|
||||||
Rules: []rbac.PolicyRule{
|
Rules: []rbac.PolicyRule{
|
||||||
{
|
{
|
||||||
APIGroups: []string {"", "extensions", "apps"},
|
APIGroups: []string{"", "extensions", "apps"},
|
||||||
Resources: []string {"pods", "services", "endpoints"},
|
Resources: []string{"pods", "services", "endpoints"},
|
||||||
Verbs: []string {"list", "get", "watch"},
|
Verbs: []string{"list", "get", "watch"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
clusterRoleBinding := &rbac.ClusterRoleBinding{
|
clusterRoleBinding := &rbac.ClusterRoleBinding{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "mizu-cluster-role-binding",
|
Name: "mizu-cluster-role-binding",
|
||||||
Labels: map[string]string{"mizu-cli-version": version},
|
Labels: map[string]string{"mizu-cli-version": version},
|
||||||
},
|
},
|
||||||
RoleRef: rbac.RoleRef{
|
RoleRef: rbac.RoleRef{
|
||||||
Name: clusterRoleName,
|
Name: clusterRoleName,
|
||||||
Kind: "ClusterRole",
|
Kind: "ClusterRole",
|
||||||
APIGroup: "rbac.authorization.k8s.io",
|
APIGroup: "rbac.authorization.k8s.io",
|
||||||
},
|
},
|
||||||
Subjects: []rbac.Subject{
|
Subjects: []rbac.Subject{
|
||||||
|
Loading…
Reference in New Issue
Block a user