Skip to content

Commit

Permalink
Move pkg/kubelet/kuberuntime/logs to k8s.io/cri-client staging
Browse files Browse the repository at this point in the history
Particulary helpful to decouple cri-tools from k/k.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed May 30, 2024
1 parent bce55b9 commit 0c9949b
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 150 deletions.
9 changes: 5 additions & 4 deletions pkg/kubelet/kuberuntime/kuberuntime_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"io"
"time"

"k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/kubelet/kuberuntime/logs"
v1 "k8s.io/api/core/v1"
"k8s.io/cri-client/pkg/logs"
"k8s.io/klog/v2"
)

// ReadLogs read the container log and redirect into stdout and stderr.
Expand All @@ -31,6 +32,6 @@ import (
func (m *kubeGenericRuntimeManager) ReadLogs(ctx context.Context, path, containerID string, apiOpts *v1.PodLogOptions, stdout, stderr io.Writer) error {
// Convert v1.PodLogOptions into internal log options.
opts := logs.NewLogOptions(apiOpts, time.Now())

return logs.ReadLogs(ctx, path, containerID, opts, m.runtimeService, stdout, stderr)
logger := klog.Background()
return logs.ReadLogs(ctx, &logger, path, containerID, opts, m.runtimeService, stdout, stderr)
}
4 changes: 0 additions & 4 deletions pkg/kubelet/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ package types
const (
// ResolvConfDefault is the system default DNS resolver configuration.
ResolvConfDefault = "/etc/resolv.conf"
// RFC3339NanoFixed is the fixed width version of time.RFC3339Nano.
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
// RFC3339NanoLenient is the variable width RFC3339 time format for lenient parsing of strings into timestamps.
RFC3339NanoLenient = "2006-01-02T15:04:05.999999999Z07:00"
)

// User visible keys for managing node allocatable enforcement on the node.
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubelet/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cri-client/pkg/logs"
)

// TODO: Reconcile custom types in kubelet/types and this subpackage
Expand All @@ -45,7 +46,7 @@ func NewTimestamp() *Timestamp {
// ConvertToTimestamp takes a string, parses it using the RFC3339NanoLenient layout,
// and converts it to a Timestamp object.
func ConvertToTimestamp(timeString string) *Timestamp {
parsed, _ := time.Parse(RFC3339NanoLenient, timeString)
parsed, _ := time.Parse(logs.RFC3339NanoLenient, timeString)
return &Timestamp{parsed}
}

Expand All @@ -57,7 +58,7 @@ func (t *Timestamp) Get() time.Time {
// GetString returns the time in the string format using the RFC3339NanoFixed
// layout.
func (t *Timestamp) GetString() string {
return t.time.Format(RFC3339NanoFixed)
return t.time.Format(logs.RFC3339NanoFixed)
}

// SortedContainerStatuses is a type to help sort container statuses based on container names.
Expand Down
43 changes: 0 additions & 43 deletions pkg/util/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package tail

import (
"bytes"
"io"
"os"
)
Expand All @@ -27,11 +26,6 @@ const (
blockSize = 1024
)

var (
// eol is the end-of-line sign in the log.
eol = []byte{'\n'}
)

// ReadAtMost reads at most max bytes from the end of the file identified by path or
// returns an error. It returns true if the file was longer than max. It will
// allocate up to max bytes.
Expand Down Expand Up @@ -59,40 +53,3 @@ func ReadAtMost(path string, max int64) ([]byte, bool, error) {
data, err := io.ReadAll(f)
return data, offset > 0, err
}

// FindTailLineStartIndex returns the start of last nth line.
// * If n < 0, return the beginning of the file.
// * If n >= 0, return the beginning of last nth line.
// Notice that if the last line is incomplete (no end-of-line), it will not be counted
// as one line.
func FindTailLineStartIndex(f io.ReadSeeker, n int64) (int64, error) {
if n < 0 {
return 0, nil
}
size, err := f.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
var left, cnt int64
buf := make([]byte, blockSize)
for right := size; right > 0 && cnt <= n; right -= blockSize {
left = right - blockSize
if left < 0 {
left = 0
buf = make([]byte, right)
}
if _, err := f.Seek(left, io.SeekStart); err != nil {
return 0, err
}
if _, err := f.Read(buf); err != nil {
return 0, err
}
cnt += int64(bytes.Count(buf, eol))
}
for ; cnt > n; cnt-- {
idx := bytes.Index(buf, eol) + 1
buf = buf[idx:]
left += int64(idx)
}
return left, nil
}
30 changes: 0 additions & 30 deletions pkg/util/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package tail

import (
"bytes"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -89,32 +88,3 @@ func TestReadAtMost(t *testing.T) {
}
}
}

func TestTail(t *testing.T) {
line := strings.Repeat("a", blockSize)
testBytes := []byte(line + "\n" +
line + "\n" +
line + "\n" +
line + "\n" +
line[blockSize/2:]) // incomplete line

for c, test := range []struct {
n int64
start int64
}{
{n: -1, start: 0},
{n: 0, start: int64(len(line)+1) * 4},
{n: 1, start: int64(len(line)+1) * 3},
{n: 9999, start: 0},
} {
t.Logf("TestCase #%d: %+v", c, test)
r := bytes.NewReader(testBytes)
s, err := FindTailLineStartIndex(r, test.n)
if err != nil {
t.Error(err)
}
if s != test.start {
t.Errorf("%d != %d", s, test.start)
}
}
}
2 changes: 2 additions & 0 deletions staging/publishing/import-restrictions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,10 @@

- baseImportPath: "./staging/src/k8s.io/cri-client"
allowedImports:
- k8s.io/api
- k8s.io/apimachinery
- k8s.io/apiserver
- k8s.io/client-go
- k8s.io/component-base
- k8s.io/cri-api
- k8s.io/cri-client
Expand Down
21 changes: 20 additions & 1 deletion staging/src/k8s.io/cri-client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ go 1.22.0

require (
github.com/Microsoft/go-winio v0.6.0
github.com/fsnotify/fsnotify v1.7.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.0
go.opentelemetry.io/otel/sdk v1.20.0
go.opentelemetry.io/otel/trace v1.20.0
golang.org/x/sys v0.20.0
google.golang.org/grpc v1.59.0
k8s.io/api v0.0.0
k8s.io/apimachinery v0.0.0
k8s.io/client-go v0.0.0
k8s.io/component-base v0.0.0
k8s.io/cri-api v0.0.0
k8s.io/klog/v2 v2.120.1
Expand All @@ -25,12 +28,25 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
Expand All @@ -53,9 +69,12 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/client-go v0.0.0 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

replace (
Expand Down
Loading

0 comments on commit 0c9949b

Please sign in to comment.