Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Local Path Provisioner
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
Local Path Provisioner
Commits
e5f0d0be
Commit
e5f0d0be
authored
2 years ago
by
Jeremy Scott
Committed by
Derek Su
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Implement function to get helper pod logs and then log them
parent
7aa7e286
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
provisioner.go
+50
-1
50 additions, 1 deletion
provisioner.go
with
50 additions
and
1 deletion
provisioner.go
+
50
−
1
View file @
e5f0d0be
package
main
import
(
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
...
...
@@ -18,6 +20,7 @@ import (
k8serror
"k8s.io/apimachinery/pkg/api/errors"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
clientset
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
pvController
"sigs.k8s.io/sig-storage-lib-external-provisioner/v8/controller"
)
...
...
@@ -556,12 +559,16 @@ func (p *LocalPathProvisioner) createHelperPod(action ActionType, cmd []string,
// If it already exists due to some previous errors, the pod will be cleaned up later automatically
// https://github.com/rancher/local-path-provisioner/issues/27
logrus
.
Infof
(
"create the helper pod %s into %s"
,
helperPod
.
Name
,
p
.
namespace
)
_
,
err
=
p
.
kubeClient
.
CoreV1
()
.
Pods
(
p
.
namespace
)
.
Create
(
context
.
TODO
(),
helperPod
,
metav1
.
CreateOptions
{})
pod
,
err
:
=
p
.
kubeClient
.
CoreV1
()
.
Pods
(
p
.
namespace
)
.
Create
(
context
.
TODO
(),
helperPod
,
metav1
.
CreateOptions
{})
if
err
!=
nil
&&
!
k8serror
.
IsAlreadyExists
(
err
)
{
return
err
}
defer
func
()
{
// log helper pod logs to the controller
if
err
:=
saveHelperPodLogs
(
pod
);
err
!=
nil
{
logrus
.
Error
(
err
.
Error
())
}
e
:=
p
.
kubeClient
.
CoreV1
()
.
Pods
(
p
.
namespace
)
.
Delete
(
context
.
TODO
(),
helperPod
.
Name
,
metav1
.
DeleteOptions
{})
if
e
!=
nil
{
logrus
.
Errorf
(
"unable to delete the helper pod: %v"
,
e
)
...
...
@@ -701,3 +708,45 @@ func createPersistentVolumeSource(volumeType string, path string) (pvs v1.Persis
return
pvs
,
nil
}
func
saveHelperPodLogs
(
pod
*
v1
.
Pod
)
(
err
error
)
{
defer
func
()
{
err
=
errors
.
Wrapf
(
err
,
"failed to save %s logs"
,
pod
.
Name
)
}()
// save helper pod logs
podLogOpts
:=
v1
.
PodLogOptions
{}
config
,
err
:=
rest
.
InClusterConfig
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"unable to retrieve in cluster config: %s"
,
err
.
Error
())
}
// creates the clientset
clientset
,
err
:=
clientset
.
NewForConfig
(
config
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"unable to get access to k8s: %s"
,
err
.
Error
())
}
req
:=
clientset
.
CoreV1
()
.
Pods
(
pod
.
Namespace
)
.
GetLogs
(
pod
.
Name
,
&
podLogOpts
)
podLogs
,
err
:=
req
.
Stream
(
context
.
TODO
())
if
err
!=
nil
{
return
fmt
.
Errorf
(
"error in opening stream: %s"
,
err
.
Error
())
}
buf
:=
new
(
bytes
.
Buffer
)
_
,
err
=
io
.
Copy
(
buf
,
podLogs
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"error in copy information from podLogs to buf: %s"
,
err
.
Error
())
}
podLogs
.
Close
()
// log all messages from the helper pod to the controller
logrus
.
Infof
(
"Start of %s logs"
,
pod
.
Name
)
bufferStr
:=
buf
.
String
()
if
len
(
bufferStr
)
>
0
{
helperPodLogs
:=
strings
.
Split
(
strings
.
Trim
(
bufferStr
,
"
\n
"
),
"
\n
"
)
for
_
,
log
:=
range
helperPodLogs
{
logrus
.
Info
(
log
)
}
}
logrus
.
Infof
(
"End of %s logs"
,
pod
.
Name
)
return
nil
}
This diff is collapsed.
Click to expand it.
Dmitriy Safronov
@zimniy
mentioned in commit
8dfb49ed
·
1 year ago
mentioned in commit
8dfb49ed
mentioned in commit 8dfb49ed03914d0ca68ee0f548d9407a38f149a9
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment