Repository: canonical/microk8s
Pull request: fix non sudo enable community addon


<!--
  Thank you for making MicroK8s better. Please fill the template below
  with more details.
-->

#### Summary
This PR fixes issues when non-root user in non-strict environment tries to enable community addons.

Closes #4574


#### Changes
added temporary setting to git to  ingnore
<!-- Please explain the list of changes made in this PR. Mention any user-facing changes. -->

#### Testing
enabled and disabled community addon

#### Possible Regressions
n/a

#### Checklist
<!-- Please verify that you have done the following -->

* [x] Read the [contributions](https://github.com/canonical/microk8s/blob/master/CONTRIBUTING.md) page.
* [x] Submitted the [CLA form](https://ubuntu.com/legal/contributors/agreement), if you are a first time contributor.
* [ ] The introduced changes are covered by unit and/or integration tests.

#### Notes
<!-- Please add any other information that you think may be relevant -->


Problem statement:
Error enabling community addon in MicroK8s 1.30.1
#### Summary
I encountered an error when trying to enable the community addon in MicroK8s version 1.30.1. The error message suggests an issue with repository ownership and permissions. This issue did not occur in MicroK8s version 1.29, where the same command worked fine.

#### What Should Happen Instead?
The community addon should be enabled without any errors, as it did in version 1.29.

#### Reproduction Steps
<!-- Are you able to consistently reproduce the issue? Please add a list of steps that lead to the bug. -->

1. Install MicroK8s version 1.30.1
2. Run the command `microk8s enable community`

#### Error Log
```
Infer repository core for addon community
Cloning into '/var/snap/microk8s/common/addons/community'...
fatal: detected dubious ownership in repository at '/snap/microk8s/current/addons/community/.git'
To add an exception for this directory, call:

git config --global --add safe.directory /snap/microk8s/current/addons/community/.git

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Traceback (most recent call last):
File "/snap/microk8s/6876/scripts/wrappers/addons.py", line 351, in <module>
addons(prog_name="microk8s addons")
File "/snap/microk8s/6876/usr/lib/python3/dist-packages/click/core.py", line 764, in **call**
return self.main(*args, **kwargs)
File "/snap/microk8s/6876/usr/lib/python3/dist-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/snap/microk8s/6876/usr/lib/python3/dist-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/snap/microk8s/6876/usr/lib/python3/dist-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/snap/microk8s/6876/usr/lib/python3/dist-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/snap/microk8s/6876/usr/lib/python3/dist-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/snap/microk8s/6876/scripts/wrappers/addons.py", line 207, in add
subprocess.check_call(cmd)
File "/snap/microk8s/6876/usr/lib/python3.8/subprocess.py", line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/snap/microk8s/6876/git.wrapper', 'clone', '/snap/microk8s/current/addons/community', PosixPath('/var/snap/microk8s/common/addons/community')]' returned non-zero exit status 128.
```

Thank you 😊

Hints from issue discussion:
Thank you for reporting this @JosePedro90 . While we are working on a fix, can you try `sudo microk8s enable community` ?
I tried using sudo and the issue remained
Got the same error.  Running the suggested command fixes the problem:
```
git config --global --add safe.directory /snap/microk8s/current/addons/community/.git
```
> Got the same error. Running the suggested command fixes the problem:
> 
> ```
> git config --global --add safe.directory /snap/microk8s/current/addons/community/.git
> ```

works for me
That worked for me as well, though I had to install Git this time, which wasn’t necessary before.

Commit message: hardcode gitconfig

Patch to review:
diff --git a/microk8s-resources/gitconfig b/microk8s-resources/gitconfig
new file mode 100644
index 0000000000..370c6b79df
--- /dev/null
+++ b/microk8s-resources/gitconfig
@@ -0,0 +1,3 @@
+[safe]
+        directory = /snap/microk8s/current/addons/community/.git
+        directory = /snap/microk8s/current/addons/core/.git
diff --git a/microk8s-resources/wrappers/git.wrapper b/microk8s-resources/wrappers/git.wrapper
index e850eb5f36..bc7729bdd3 100755
--- a/microk8s-resources/wrappers/git.wrapper
+++ b/microk8s-resources/wrappers/git.wrapper
@@ -8,4 +8,6 @@ export GIT_EXEC_PATH="$SNAP/usr/lib/git-core"
 export GIT_TEMPLATE_DIR="$SNAP/usr/share/git-core/templates"
 export GIT_CONFIG_NOSYSTEM=1
 
+HOME="${SNAP_COMMON}"
+
 "$SNAP/usr/bin/git" "${@}"
diff --git a/snap/hooks/install b/snap/hooks/install
index dbaeebb2e4..3b35489c3c 100755
--- a/snap/hooks/install
+++ b/snap/hooks/install
@@ -148,4 +148,7 @@ then
   touch ${SNAP_DATA}/var/lock/low-memory-guard.lock
 fi
 
+# copy git config
+cp ${SNAP}/gitconfig  ${SNAP_COMMON}/.gitconfig
+
 touch "${SNAP_DATA}/var/lock/installed.lock"
diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index 44d69780c2..492160a704 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -250,6 +250,7 @@ parts:
       cp client.config.template "${SNAPCRAFT_PART_INSTALL}/client.config.template"
       cp kubelet.config.template "${SNAPCRAFT_PART_INSTALL}/kubelet.config.template"
       cp client-x509.config.template "${SNAPCRAFT_PART_INSTALL}/client-x509.config.template"
+      cp gitconfig "${SNAPCRAFT_PART_INSTALL}/gitconfig"
 
       cp -r wrappers/* "${SNAPCRAFT_PART_INSTALL}/"
       cp -r actions/ "${SNAPCRAFT_PART_INSTALL}/actions"


Code review comments:
diff_hunk: @@ -250,6 +250,7 @@ parts:
       cp client.config.template "${SNAPCRAFT_PART_INSTALL}/client.config.template"
       cp kubelet.config.template "${SNAPCRAFT_PART_INSTALL}/kubelet.config.template"
       cp client-x509.config.template "${SNAPCRAFT_PART_INSTALL}/client-x509.config.template"
+      cp gitconfig "${SNAPCRAFT_PART_INSTALL}/gitconfig"
path: snap/snapcraft.yaml
text: @user1:
We would not need this since `default-args` is copied above.

diff_hunk: @@ -148,4 +148,7 @@ then
   touch ${SNAP_DATA}/var/lock/low-memory-guard.lock
 fi
 
+# copy git config
+cp ${SNAP}/gitconfig  ${SNAP_COMMON}/.gitconfig
+
path: snap/hooks/install
text: @user1:
```suggestion
# copy git config
mkdir -p ${SNAP_DATA}/args/git
cp ${SNAP}/default-args/git/.gitconfig  ${SNAP_DATA}/args/git/.gitconfig
```

Let's put this under `/var/snap/microk8s/current/args/git`.

diff_hunk: @@ -8,4 +8,6 @@ export GIT_EXEC_PATH="$SNAP/usr/lib/git-core"
 export GIT_TEMPLATE_DIR="$SNAP/usr/share/git-core/templates"
 export GIT_CONFIG_NOSYSTEM=1
 
+HOME="${SNAP_COMMON}"
path: microk8s-resources/wrappers/git.wrapper
text: @user1:
```suggestion
HOME="${SNAP_DATA}/args/git"
```

Let's put this under the `args` folder.