-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kubevirt zedbox service zedkube #3827
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,101 @@ | ||
// Copyright (c) 2024 Zededa, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build kubevirt | ||
|
||
package zedkube | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"io" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/lf-edge/eve/pkg/pillar/base" | ||
"github.com/lf-edge/eve/pkg/pillar/kubeapi" | ||
"github.com/lf-edge/eve/pkg/pillar/types" | ||
"github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// collectAppLogs - collect App logs from pods which covers both containers and virt-launcher pods | ||
func collectAppLogs(ctx *zedkubeContext) { | ||
sub := ctx.subAppInstanceConfig | ||
items := sub.GetAll() | ||
if len(items) == 0 { | ||
return | ||
} | ||
|
||
if ctx.config == nil { | ||
config, err := kubeapi.GetKubeConfig() | ||
if err != nil { | ||
return | ||
} | ||
ctx.config = config | ||
} | ||
clientset, err := kubernetes.NewForConfig(ctx.config) | ||
if err != nil { | ||
log.Errorf("collectAppLogs: can't get clientset %v", err) | ||
return | ||
} | ||
|
||
// "Thu Aug 17 05:39:04 UTC 2023" | ||
timestampRegex := regexp.MustCompile(`(\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \w+ \d{4})`) | ||
nowStr := time.Now().String() | ||
|
||
var sinceSec int64 | ||
sinceSec = logcollectInterval | ||
for _, item := range items { | ||
aiconfig := item.(types.AppInstanceConfig) | ||
contName := base.GetAppKubeName(aiconfig.DisplayName, aiconfig.UUIDandVersion.UUID) | ||
|
||
opt := &corev1.PodLogOptions{} | ||
if ctx.appLogStarted { | ||
opt = &corev1.PodLogOptions{ | ||
SinceSeconds: &sinceSec, | ||
} | ||
} else { | ||
ctx.appLogStarted = true | ||
} | ||
req := clientset.CoreV1().Pods(kubeapi.EVEKubeNameSpace).GetLogs(contName, opt) | ||
podLogs, err := req.Stream(context.Background()) | ||
if err != nil { | ||
log.Errorf("collectAppLogs: pod %s, log error %v", contName, err) | ||
continue | ||
} | ||
defer podLogs.Close() | ||
|
||
scanner := bufio.NewScanner(podLogs) | ||
for scanner.Scan() { | ||
logLine := scanner.Text() | ||
|
||
matches := timestampRegex.FindStringSubmatch(logLine) | ||
var timeStr string | ||
if len(matches) > 0 { | ||
timeStr = matches[0] | ||
ts := strings.Split(logLine, timeStr) | ||
if len(ts) > 1 { | ||
logLine = ts[0] | ||
} | ||
} else { | ||
timeStr = nowStr | ||
} | ||
// Process and print the log line here | ||
aiLogger := ctx.appContainerLogger.WithFields(logrus.Fields{ | ||
"appuuid": aiconfig.UUIDandVersion.UUID.String(), | ||
"containername": contName, | ||
"eventtime": timeStr, | ||
}) | ||
aiLogger.Infof("%s", logLine) | ||
} | ||
if scanner.Err() != nil { | ||
if scanner.Err() != io.EOF { | ||
log.Errorf("collectAppLogs: pod %s, scanner error %v", contName, scanner.Err()) | ||
} | ||
break // Break out of the loop when EOF is reached or error occurs | ||
} | ||
} | ||
} |
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,99 @@ | ||
// Copyright (c) 2024 Zededa, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build kubevirt | ||
|
||
package zedkube | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lf-edge/eve/pkg/pillar/kubeapi" | ||
"github.com/lf-edge/eve/pkg/pillar/types" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// checkIoAdapterEthernet - check and create NAD for direct-attached ethernet | ||
func checkIoAdapterEthernet(ctx *zedkubeContext, aiConfig *types.AppInstanceConfig) error { | ||
|
||
if aiConfig.FixedResources.VirtualizationMode != types.NOHYPER { | ||
return nil | ||
} | ||
ioAdapter := aiConfig.IoAdapterList | ||
for _, io := range ioAdapter { | ||
if io.Type == types.IoNetEth { | ||
nadname := "host-" + io.Name | ||
_, ok := ctx.networkInstanceStatusMap.Load(nadname) | ||
if !ok { | ||
bringupInterface(io.Name) | ||
err := ioEtherCreate(ctx, &io) | ||
if err != nil { | ||
log.Errorf("checkIoAdapterEthernet: create io adapter error %v", err) | ||
} | ||
ctx.ioAdapterMap.Store(nadname, true) | ||
log.Functionf("ccheckIoAdapterEthernet: nad created %v", nadname) | ||
} else { | ||
log.Functionf("checkIoAdapterEthernet: nad already exist %v", nadname) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func checkDelIoAdapterEthernet(ctx *zedkubeContext, aiConfig *types.AppInstanceConfig) { | ||
|
||
if aiConfig.FixedResources.VirtualizationMode != types.NOHYPER { | ||
return | ||
} | ||
ioAdapter := aiConfig.IoAdapterList | ||
for _, io := range ioAdapter { | ||
if io.Type == types.IoNetEth { | ||
nadname := "host-" + io.Name | ||
_, ok := ctx.ioAdapterMap.Load(nadname) | ||
if ok { | ||
// remove the syncMap entry | ||
ctx.ioAdapterMap.Delete(nadname) | ||
} | ||
// delete the NAD in kubernetes | ||
kubeapi.DeleteNAD(log, nadname) | ||
log.Functionf("checkDelIoAdapterEthernet: delete existing nad %v", nadname) | ||
} | ||
} | ||
} | ||
|
||
// ioEtherCreate - create and send NAD for direct-attached ethernet | ||
func ioEtherCreate(ctx *zedkubeContext, ioAdapt *types.IoAdapter) error { | ||
name := ioAdapt.Name | ||
spec := fmt.Sprintf( | ||
`{ | ||
"cniVersion": "0.3.1", | ||
"plugins": [ | ||
{ | ||
"type": "host-device", | ||
"device": "%s" | ||
} | ||
] | ||
}`, name) | ||
|
||
err := kubeapi.CreateOrUpdateNAD(log, "host-"+name, spec) | ||
if err != nil { | ||
log.Errorf("ioEtherCreate: spec, CreateOrUpdateNAD, error %v", err) | ||
} else { | ||
log.Functionf("ioEtherCreate: spec, CreateOrUpdateNAD, done") | ||
} | ||
return err | ||
} | ||
|
||
func bringupInterface(intfName string) { | ||
link, err := netlink.LinkByName(intfName) | ||
if err != nil { | ||
log.Errorf("bringupInterface: %v", err) | ||
return | ||
} | ||
|
||
// Set the IFF_UP flag to bring up the interface | ||
if err := netlink.LinkSetUp(link); err != nil { | ||
log.Errorf("bringupInterface: %v", err) | ||
return | ||
} | ||
} |
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,45 @@ | ||
// Copyright (c) 2024 Zededa, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build !kubevirt | ||
|
||
package zedkube | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/lf-edge/eve/pkg/pillar/agentbase" | ||
"github.com/lf-edge/eve/pkg/pillar/base" | ||
"github.com/lf-edge/eve/pkg/pillar/pubsub" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
agentName = "zedkube" | ||
// Time limits for event loop handlers | ||
errorTime = 3 * time.Minute | ||
warningTime = 40 * time.Second | ||
stillRunningInterval = 25 * time.Second | ||
) | ||
|
||
type zedkubeContext struct { | ||
agentbase.AgentBase | ||
} | ||
|
||
// Run in this file is just stub for non-kubevirt hypervisors. | ||
func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, arguments []string) int { | ||
logger := loggerArg | ||
log := logArg | ||
|
||
zedkubeCtx := zedkubeContext{} | ||
agentbase.Init(&zedkubeCtx, logger, log, agentName, | ||
agentbase.WithPidFile(), | ||
agentbase.WithWatchdog(ps, warningTime, errorTime), | ||
agentbase.WithArguments(arguments)) | ||
|
||
stillRunning := time.NewTicker(stillRunningInterval) | ||
for range stillRunning.C { | ||
ps.StillRunning(agentName, warningTime, errorTime) | ||
} | ||
return 0 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it is an error, why store the
nadname
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in here, there is a case the NAD already exist on the kubernetes side, we want to count this as created but log an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in another outstanding PR #3841, we have added the function 'func CheckEtherPassThroughNAD(nadName string)', we can later to add a check to see if it already exists condition if not, then do the 'continue' as suggested.